最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Js Promise.all not returning properly - Stack Overflow

programmeradmin7浏览0评论

I have several functions that I would like to execute in parallel via Promises (loadA, loadB, loadC).

Promise.all([
  new Promise(loadA),
  new Promise(loadB),
  new Promise(loadC)
])
.then(checkFunction)
.catch(errorHandler);

The functions look like:

function loadA(){
   return load("A");
}

where A is replaced by B and C where appropriate. The load function is:

function load(value) {
  return new Promise(function(resolve, reject) {
    chrome.storage.sync.get(value, function (result) {
      if(!exists(result)){
        console.log("tried to fetch " + value + " from sync, could not find");
        reject("could not find the user information");
      } else {
        console.log("fetched " + value + " from sync: " + result.userId);
        resolve(result);
      }
    });
  })
}

```

I am doing some testing and when the load function sends the Promise reject, something goes wrong. The function 'next-in-line' is checkFunction and it is not called and neither is the errorHandler function. I have confirmed that the load promise is actually rejecting because the console shows 3 'tried to fetch...' statements.

I also don't understand why I am seeing 3 tried to fetch.. statements since the specification for Promise.all states that it will return as soon as one of the promises rejects or after all promises are resolved (source: ). My observed behavior is not matching either of these scenarios, what might I be doing wrong?

I have several functions that I would like to execute in parallel via Promises (loadA, loadB, loadC).

Promise.all([
  new Promise(loadA),
  new Promise(loadB),
  new Promise(loadC)
])
.then(checkFunction)
.catch(errorHandler);

The functions look like:

function loadA(){
   return load("A");
}

where A is replaced by B and C where appropriate. The load function is:

function load(value) {
  return new Promise(function(resolve, reject) {
    chrome.storage.sync.get(value, function (result) {
      if(!exists(result)){
        console.log("tried to fetch " + value + " from sync, could not find");
        reject("could not find the user information");
      } else {
        console.log("fetched " + value + " from sync: " + result.userId);
        resolve(result);
      }
    });
  })
}

```

I am doing some testing and when the load function sends the Promise reject, something goes wrong. The function 'next-in-line' is checkFunction and it is not called and neither is the errorHandler function. I have confirmed that the load promise is actually rejecting because the console shows 3 'tried to fetch...' statements.

I also don't understand why I am seeing 3 tried to fetch.. statements since the specification for Promise.all states that it will return as soon as one of the promises rejects or after all promises are resolved (source: http://mzl.la/1jLTOHB). My observed behavior is not matching either of these scenarios, what might I be doing wrong?

Share Improve this question asked Jun 25, 2014 at 18:17 Nathaniel WendtNathaniel Wendt 1,2024 gold badges24 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

With your load function already creating and returning a promise, it looks to me like you just want this:

Promise.all([
  loadA(),
  loadB(),
  loadC()
])
.then(checkFunction)
.catch(errorHandler);

The way you've structured your code, you are starting three operations that run in parallel to load "A", "B" and "C". Those three operations run independently and thus why you see three "tried to fetch..." messages (one from inside of each operation). It is only outside of those three independent operations that Promise.all() will look at the resolve/reject status and decide what to do next.


And, in fact, you could simplify to this:

Promise.all([
  load("A"),
  load("B"),
  load("C")
])
.then(checkFunction)
.catch(errorHandler);

The way you had it, the outer promises you were creating were never getting resolved or rejected so .then() or .catch() never fired. Only, the inner promises inside of your load() function were getting resolved or rejected.

发布评论

评论列表(0)

  1. 暂无评论