Created
January 3, 2018 07:04
-
-
Save sushiljainam/b95ea3fbfdfb4a7b5bd8042fc2e3cc0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @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