How to use the kew.allSettled function in kew

To help you get started, we’ve selected a few kew examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Medium / zcache / lib / MultiplexingCache.js View on Github external
} else {
      keysToFetch.push(key)
    }
  }

  // If any keys still need fetching then do an actual mget for them.
  if (keysToFetch.length > 0) {
    var mgetPromise = this._mget(keysToFetch)
    promisesToWaitFor.push(mgetPromise)
    for (var j = 0; j < keysToFetch.length; j++) {
      this._pendingGets[keysToFetch[j]] = mgetPromise
    }
  }

  // Wait for all the promises to complete and aggregate the results.
  return Q.allSettled(promisesToWaitFor).thenBound(function (maps) {
    // Flatten the results from each promise.  In terms of Big-O this is less
    // efficient than building the result map using promises, but is less heavy.
    var aggregatedData = {}
    var aggregatedErrors = {}
    for (var i = 0; i < maps.length; i++) {
      if (maps[i]["state"] == "fulfilled") {
        for (var resultKey in maps[i]["value"]) {
          aggregatedData[resultKey] = maps[i]["value"][resultKey]
        }
      } else if (maps[i]["reason"]) {
          // The promise was not fulfilled. Set the error.
        aggregatedErrors[key] = maps[i]["reason"]
      }
    }
    if (Object.keys(aggregatedErrors).length == 0) {
      var result = []
github Medium / zcache / test / test_MultiplexingCache.js View on Github external
builder.add(function testConcurrentMGetsWithExceptions(test) {
  fake.setSync('a', '1')
  fake.setSync('b', '2')
  fake.setSync('c', '3')
  fake.setSync('d', '4')
  fake.setSync('e', '5')
  fake.setSync('f', '6')
  fake.setSync('g', '7')

  fake.setFailureCount(1)
  var p1 = cache.mget(['a', 'b', 'c'])
  var p2 = cache.mget(['b', 'c', 'd'])
  var p3 = cache.mget(['e', 'f', 'g'])
  return Q.allSettled([p1, p2, p3]).then(function (results) {
    test.equal(results[0]["state"], "rejected")
    test.equal(results[1]["state"], "rejected")
    test.equal(results[2]["state"], "fulfilled")
    test.deepEqual(results[2]["value"], ['5', '6', '7'])
    test.ok(results[0]["reason"] instanceof PartialResultError)
    var p0Result = results[0]["reason"].getData()
    test.ok(results[1]["reason"] instanceof PartialResultError)
    var p1Result = results[1]["reason"].getData()
    test.equal(p0Result['a'], undefined)
    test.equal(p0Result['b'], undefined)
    test.equal(p0Result['c'], undefined)
    test.equal(p1Result['b'], undefined)
    test.equal(p1Result['c'], undefined)
    test.equal(p1Result['d'], 4)
  })
})
github Medium / zcache / lib / CacheCluster.js View on Github external
keys.forEach(function (key) {
    var uri = self._hashRing.get(key)
    if (!(uri in keysPerInstance)) keysPerInstance[uri] = []
    keysPerInstance[uri].push(key)
  })

  var promises = []
  for (var uri in keysPerInstance) {
    var keysOnInstance = keysPerInstance[uri]
    promises.push(this._servers[uri].mget(keysOnInstance)
      .then(setValues.bind(null, values, keysOnInstance))
      .fail(setError.bind(null, errors, keysOnInstance))
    )
  }

  var promise = Q.allSettled(promises)
    .then(function() {
      if (Object.keys(errors).length === 0) {
        return keys.map(function (key) {return values[key]})
      } else {
        self._getPartialFailureCounter('mget').inc()
        throw new PartialResultError(values, errors)
      }
    })

  return this._wrapPromiseWithProfiling(promise, 'mget')
}

kew

a lightweight promise library for node

Apache-2.0
Latest version published 9 years ago

Package Health Score

59 / 100
Full package analysis