Q Promise API Simple Translation

Source: Internet
Author: User

Detail Api:https://github.com/kriskowal/q/wiki/api-reference

Q provides a way to implement promise, which is now much more in node. Because there is no Chinese API, I will simply translate it. Welcome to shoot Bricks ...

First, Core Promise Methods

1, Promise.then (onfulfilled, onrejected, OnProgress)

Let's not talk about this.

2, Promise.catch (onrejected)

It is equivalent to: promise.then (undefined, onrejected) means execution when catch to exception.

3, promise.progress (onprogress)

Equivalent to: promise.then (undefined, undefined, onprogress)???

4, promise.finally (callback)

Similar to Trycatch in the finally, regardless of success or not will be executed callback, mainly used to close the database, shut down the service, delete a useless key and so on. Finally, the promise is returned to perform exactly the same promise (including failure and success), but if callback also returns promise, then the former will wait until the latter occurs before parsing.

5, Promise.done (onfulfilled, onrejected, OnProgress)

This method is much like hate, but if there is an unhandled rejection or its onrejected undefined, this method is useful. The official explanation is that this method can be used to end the promise chain to prevent promise from continuing to pass.

Second, Promise-for-object Methods

This type of approach is relatively simple:

1, Promise.get (PropertyName)

Equivalent as follows, does not explain:

Promise.then (function (o) {    return o[propertyname];});  

2, Promise.post (MethodName, args)

The equivalence is as follows: not explained:

Promise.then (function (o) {    return o[methodname].  Apply (o, args);});


3.promise.invoke (methodName, ... args)

Similar to the previous one, not explained, the difference is that the parameter is not an array but is assigned directly.

4, Promise.keys ()

The equivalence is as follows: not explained:

Promise.then (function (o) {    object.keys (o);});  

Third, Promise-for-function Methods

1, Promise.fbind (... args) (deprecated)

I don't see what I mean.

2. promise.fapply (args)

Equivalent to:

Promise.then (function (f) {    return F.apply (undefined, args);});    

This means that promise returns a function and then calls it.

3. Promise.fcall (.... args)

Iv. Promise-for-array Methods

1, Promise.all ()

This method is useful in that it accepts a promise array and only succeeds if each promise in the array succeeds and returns the result as an array. When there is a promise failure, that fails. This is to ensure that the promise in the array are successful. Typical examples:

Q. All([Getfromdisk (), Getfromcloud ()]). Do (values) {    assert (values[= = = values[/ /values[0] is Fromdisk and values[1] is fromcloud});    

2,promise.allsettled ()

Ah ah ah ah ... Not fully understand!

3, Promise.spread (onfulfilled, onrejected)

This method is similar to then, but when an array of promise has a failure, it will execute onrejected with the reject reason of the first failed promise ... So it works with all with more, such as:

Q. All([Getfromdisk (), Getfromcloud ()]). Spread (function (cloudval) {    = = = Cloudval);}). Done ();   

Wu, Utility Methods

1, Promise.thenresolve (value)

Equivalent to: promise.then(function () { return value; }) .

2, promise.thenreject (reason)

Equivalent to: promise.then(function () { throw reason; }) .

3, Promise.timeout (MS, message)

Normally, the promise result is returned normally, but if promise does not finish before Ms milliseconds, it returns a message rejection, if the message is not given, returns:. For "Timed out after " + ms + " ms" Example:

 promise.timeout (10000). Then (function (result) { //would be called if the Promise resolves normally console.l OG (result); }, function (err) {//would be called if the promise was rejecte D, or the second timeout occurs console.log (err);}); 
promise.delay (ms)
Note that this is the normal return of a normal value only if the promise is executed after Ms milliseconds.
5, Q.delay (ms)
Q.delay. Then (dosomething); equivalent to settimeout

Six, Promise Creation
1, Q.defer ()

Returns a "deferred" object with a:

    • promiseProperty
    • resolve(value)Method
    • reject(reason)Method
    • notify(value)Method
    • makeNodeResolver()Method




















Q Promise API Simple translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.