Skip to content

Instantly share code, notes, and snippets.

@noomz
Created June 11, 2014 02:49
Show Gist options
  • Save noomz/b551f22ed11f729ec215 to your computer and use it in GitHub Desktop.
Save noomz/b551f22ed11f729ec215 to your computer and use it in GitHub Desktop.
When example
'use strict';
var when = require('when');
var check = function(value) {
// return promise here and then ready to run next expression without waiting.
return when.promise(function(resolve, reject) {
// simulate delay task.
setTimeout(function() {
if (value === 0) {
// use reject() to throw and error.
reject(new Error('Zero value is not allowed'));
}
// call resolve() to return the result.
resolve( 'Check ' + value );
}, 1000);
});
};
when
.map([1, 2, 0], function(value) {
return check(value);
})
// use `then` to run next command when the promised task is already finish.
.then(function(results) {
console.log('The result is:', results.join(', '));
})
// catch error here.
.catch(function(err) {
console.log('There is an error:', err);
})
// clearing things.
.finally(function() {
console.log('Done!');
});
@tonkla
Copy link

tonkla commented Jun 11, 2014

โอ้โห ตัวอย่างนี้อันนี้โหดสัสอ่ะครับ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment