RSVP is another node module which implements Promises/A+ .
It provides all the usual promise features, in addition to that it
gives hashSettled()
which contains the status and data of promise(s)
irrespective of it being resolved or rejected.
First things first, do npm install rsvp
var rsvp = require ( ' rsvp ' );
A fake xhr
var someAsyncFunction = function ( letsResolveIt , resolve , reject ) {
setTimeout ( function () {
if ( letsResolveIt ) {
resolve ({
name : ' mohan ' ,
age : 55
});
} else {
reject ({
msg : ' could not find the info '
});
}
}, 3000 );
};
Basic promise
promise = new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( true , resolve , reject );
});
promise . then ( function ( data ) {
// letsResolveIt is true
console . log ( data );
}, function ( err ) {
// letsResolveIt is false
console . log ( err );
});
Chaining
promise = new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( true , resolve , reject );
});
promise . then ( function ( data ) {
// letsResolveIt is true
console . log ( ' Passing to 2nd chain ' );
return data ;
}). then ( function ( data ) {
// letsResolveIt is true
console . log ( ' Passing to 3rd chain ' );
return data ;
}). then ( function ( data ) {
// letsResolveIt is true
console . log ( ' Bored of chaining! ' );
console . log ( data );
});
Catch
promise = new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( false , resolve , reject );
});
promise . then ( function ( data ) {
console . log ( ' 1st then ' );
}). then ( function ( data ) {
console . log ( ' 2nd then ' );
}). catch ( function ( err ) {
// handles error of first or second `then`
console . log ( ' Err: ' , err );
});
Finally: as the semantics dictate
promise = new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( false , resolve , reject );
});
promise . then ( function ( data ) {
// letsResolveIt is true
console . log ( data );
}, function ( err ) {
// letsResolveIt is false
console . log ( err );
}). finally ( function () {
console . log ( ' Cannot stop me from running. ' );
})
Hash of promises (all)
var promises = {
onePromise : new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( true , resolve , reject );
}),
anotherPromise : new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( false , resolve , reject );
}),
lastPromise : new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( true , resolve , reject );
})
};
rsvp . hash ( promises ). then ( function ( data ) {
// Will only come here when every promise is fulfilled
// in our case, it's not
console . log ( data . onePromise );
console . log ( data . anotherPromise );
console . log ( data . lastPromise );
});
Hash settled (all settled)
var promises = {
onePromise : new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( true , resolve , reject );
}),
anotherPromise : new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( false , resolve , reject );
}),
lastPromise : new rsvp . Promise ( function ( resolve , reject ) {
someAsyncFunction ( true , resolve , reject );
})
};
rsvp . hashSettled ( promises ). then ( function ( data ) {
console . log ( data );
// Even if any promise is not fulfilled
// Output:
// {
// onePromise: {
// state: 'fulfilled',
// value: {
// name: 'mohan',
// age: 55
// }
// },
// anotherPromise: {
// state: 'rejected',
// reason: {
// msg: 'could not find the info'
// }
// },
// lastPromise: {
// state: 'fulfilled',
// value: {
// name: 'mohan',
// age: 55
// }
// }
// }
});