I started going to the front less than a year, remember to go to a company interview, had not yet come into contact with ES6, let alone use at work, just to stay in for the promise glance at a document. At that time I remember the interviewer is asking, js finish first thing to realize 1, then 2 things, the last thing 3. He was thought for a moment with a handwritten es5 event callback hell about this issue,
1 2 3 4 5 6 7 8 9
function () { do something1 functionstep2() { dong something2 functionstep3 (){ do something3 } } }
Then put a bit less mouth can also use the promise, the results look the interviewer requested handwriting, he said at the time cool after use es6 now look back, in fact, a promise is not difficult to achieve, but more simple
the let State = . 1 ; function ( Resolve, Reject ) { Console .log ( 'the first step' ); IF (State == . 1 ) { Resolve ( 'the first step is executed successfully' ) } the else { Reject ( 'the first step failed ' ) } }
function Step3 ( Resolve, Reject ) { Console .log ( 'the third step' ); IF (State == . 1 ) { Resolve ( 'the third step is executed successfully' ) } the else { Reject ( 'the third step failed' ) }}