Skip to content

Instantly share code, notes, and snippets.

@tony1223
Created August 9, 2012 00:18
Show Gist options
  • Select an option

  • Save tony1223/3299930 to your computer and use it in GitHub Desktop.

Select an option

Save tony1223/3299930 to your computer and use it in GitHub Desktop.
/* 以他的例子來說,*/
getThingFromHardDrive(function(thing) {
uploadToServer(thing, function(response) {
saveToLogDatabase(response, function(logResponse) {
getAnotherThingFromHardDrive(function(thing) {
// etc
})
})
})
})
/* 可以串成 */
(function(){
var promise = new Promise();
getThingFromHardDrive(function(thing) {
promise.resolve(thing);
})
promise.then(function(thing){
var nextpromise = new Promise();
uploadToServer(thing, function(response) {
nextpromise.resolve(response);
});
return nextpromise;
}).then(function(response){
var nextpromise = new Promise();
saveToLogDatabase(response, function(logResponse) {
nextpromise.resolve(logResponse);
})
return nextpromise;
}).then(function(logResponse){
var nextpromise = new Promise();
getThingFromHardDrive(function(thing) {
nextpromise.resolve(thing);
});
return nextpromise;
});;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment