Skip to content

Instantly share code, notes, and snippets.

@sushiljainam
Created January 3, 2018 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sushiljainam/b95ea3fbfdfb4a7b5bd8042fc2e3cc0f to your computer and use it in GitHub Desktop.
Save sushiljainam/b95ea3fbfdfb4a7b5bd8042fc2e3cc0f to your computer and use it in GitHub Desktop.
/*
* @Author: sushiljainam
* @Date: 2018-01-03 12:20:39
* @Last Modified by: sushiljainam
* @Last Modified time: 2018-01-03 12:31:37
*/
//----------------------- common
var afterLongWork = function () {
console.log('after longWork do this')
}
//------------------------- with callback
function longWork(cb) {
setTimeout(function () {
console.log('longWork finished');
cb()
}, 2000);
}
longWork(afterLongWork)
//------------------------ promise like
function longWork() {
var self = this;
this.then = function (cb) {
this.afterSuccess = cb;
}
setTimeout(function () {
console.log('longWork finished');
self.afterSuccess();
}, 2000)
return this;
}
longWork().then(afterLongWork);
//------------------------ hybrid
function longWork(cbg) {
var self = this;
this.afterSuccess = cbg;
this.then = function (cb) {
this.afterSuccess = cb;
}
setTimeout(function () {
console.log('longWork finished');
self.afterSuccess();
}, 2000)
return this;
}
longWork(afterLongWork)
longWork().then(afterLongWork);
//------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment