简体   繁体   中英

Meaning of done function kriskowal's q

The done function is used to end a chain of promises, like:

foo()
.then(function () {
    return "bar";
})
.done();

From the project link: https://github.com/kriskowal/q :

When you get to the end of a chain of promises, you should either return the last promise or end the chain.

What exactly does "end the chain" mean? What are the implications?

This site has a great explanation

http://www.mattgreer.org/articles/promises-in-wicked-detail/

Here is the text under the topic: done() to the Rescue

done() can be called whenever then() can. The key differences are it does not return a promise, and any unhandled exception inside of done() is not captured by the promise implementation. In other words, done() represents when the entire promise chain has fully resolved.

And the code example used in the site's example.

 getSomeJson().done(function(json) {
  // when this throws, it won't be swallowed
  var obj = JSON.parse(json);
  console.log(obj);
});

Further reading from q's github https://github.com/kriskowal/q/wiki/API-Reference#promisedoneonfulfilled-onrejected-onprogress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM