Created
July 23, 2017 01:19
-
-
Save americanstone/dfa7eda1690d7b3835ddf09a9e606aee to your computer and use it in GitHub Desktop.
closure
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
| (function(){ | |
| var sequence = Promise.resolve(); | |
| for(var i = 0; i < 10 ; i++){ | |
| const timeout = Math.random() * 1000; | |
| sequence = sequence.then((function(icopy){ | |
| return new Promise((resolve, reject) => {setTimeout(() => { console.log(icopy); resolve();}, timeout);}); | |
| })(i)); | |
| } | |
| })() | |
| (function(){ | |
| var sequence = Promise.resolve(); | |
| [1,2,3,4,5,6].forEach(i => { | |
| const timeout = Math.random() * 1000; | |
| sequence = sequence.then((function(icopy){ | |
| new Promise( (resolve, reject) => { console.log(icopy); resolve();}); | |
| })(i)); | |
| }); | |
| })() | |
| (function(){ | |
| [1,2,3,4,5,6].forEach(i => { | |
| const timeout = Math.random() * 1000; | |
| setTimeout(() => console.log(i), timeout) | |
| }); | |
| console.log("nihao"); | |
| })() | |
| (function(){ | |
| [1,2,3,4,5,6].forEach(function(i){ | |
| const timeout = Math.random() * 1000; | |
| setTimeout(function(){console.log(i)}, timeout) | |
| }); | |
| })() | |
| (function(){ | |
| for(const i = 0; i < 10 ; i++){ | |
| const timeout = Math.random() * 1000; | |
| setTimeout(() => console.log(i), timeout); | |
| } | |
| })() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment