Promise.join
promise.join ( Promise<any>| Any values ..., function--Promise
For coordinating multiple concurrent discrete promises. .all
while are good for handling a dynamically sized list of uniform promises, are much easier (and more Promise.join
performant) To use when you have a fixed amount of discrete promises, that's want to coordinate concurrently. The final parameter, handler function, 'll be invoked with the result values of all of the fufilled promises. For example:
var Promise = require ("Bluebird"); var join = promise.join;join (getpictures (), getcomments (), Gettweets (), function (Pictures, comments, tweets) { Console.log ("in total:" + pictures.length + comments.length + tweets.length);});
varPromise = require ("Bluebird");varFS = Promise.promisifyall (Require ("FS"));varpg = require ("PG"); Promise.promisifyall (PG, {filter:function(methodName) {returnMethodName = = = "Connect"}, Multiargs:true});//Promisify Rest of PG normallyPromise.promisifyall (PG);varJoin =Promise.join;varconnectionString = "Postgres://username:[email protected]lhost/database";varFcontents = Fs.readfileasync ("file.txt", "UTF8");varFstat = Fs.statasync ("file.txt");varFsqlclient = Pg.connectasync (connectionString). Spread (function(client, done) {Client.close=Done ; returnclient;}); Join (fcontents, Fstat, Fsqlclient,function(contents, Stat, sqlClient) {varquery = "INSERT into Files (bytesize, contents) VALUES ($, $)"; returnsqlclient.queryasync (query, [stat.size, Contents]). Thenreturn (query);}). Then (function(query) {Console.log ("Successfully ran the Query:" +( query);}).finally(function() { //This is what you want to use promise.using for resource management if(fsqlclient.isfulfilled ()) {Fsqlclient.value (). Close (); }});
Note:in 1.x and 0.x Promise.join
used to BES A that took the values in as Promise.all
arguments instead of an array. This behavior have been deprecated but was still supported Partially-when the last argument was an immediate function value The new semantics would apply
Promise.try
Promise. Try (function() FN), Promise
Promise.attempt (function() FN), Promise
Start the chain of promises with Promise.try
. Any synchronous exceptions'll be turned to rejections on the returned promise.
function Getuserbyid (ID) { return Promise. Try(function() { if (typeof ID!== "number") { ThrowNew Error ("ID must be a number"); } return Db.getuserbyid (ID); });}
Now if someone uses this function, they'll catch all errors in their Promise handlers instead of have to .catch
handle Both synchronous and asynchronous exception flows.
For compatibility with earlier ECMAScript version, an alias is Promise.attempt
provided for Promise.try
.
Promise.method
Promise.method (functionfunction
Returns a new function that wraps the given function fn
. The new function'll always return a promise that's fulfilled with the original functions return values or rejected with Thrown exceptions from the original function.
This method was convenient when a function can sometimes return synchronously or throw synchronously.
Example without using Promise.method
:
MyClass.prototype.method =function(input) {if(! This. IsValid (input)) { returnPromise.reject (NewTypeError ("Input is not valid")); } if( This. Cache (Input) { returnPromise.resolve ( This. Somecachedvalue); } returnDb.queryasync (Input). bind ( This). Then (function(value) { This. Somecachedvalue =value; returnvalue; });};
Using the same function Promise.method
, there is no need-manually wrap direct return or throw values into a promise:
MyClass.prototype.method = Promise.method (function(input) {if(! This. IsValid (input)) { Throw NewTypeError ("Input is not valid"); } if( This. Cache (Input) { return This. Somecachedvalue; } returnDb.queryasync (Input). bind ( This). Then (function(value) { This. Somecachedvalue =value; returnvalue; });});
Promise.resolve
Promise.resolve (promise<any>|any value), Promise
Create a promise that's resolved with the given value. If value
is already a trusted Promise
, it's returned as is. If value
is not a thenable, a fulfilled Promise are returned with as its value
fulfillment value. If is value
a thenable (Promise-like object, like those returned by JQuery's $.ajax
), returns a trusted Promise that Assimi Lates the state of the thenable.
Example: (Is $
jQuery)
Promise.resolve ($.get ("http://www.google.com")). Then (function() { // Returning a thenable from a handler are automatically //cast to a trusted Promise as per promises/a+ Specification return $.post ("http://www.yahoo.com");}). Then (function() {}). Catch (function(e) { //jQuery doesn ' t throw real errors so use Catch-all Console.log (E.statustext);});
Promise.reject
Create a promise that's rejected with the given error
.
Bluebird-core API (iii)