The afternoon sun is drowsy, and last week 51 kind of confused, see a code half a day did not see understand, just unconsciously narrowed a few minutes, wake up and then see Wake up.
This code loads the Story.json file first, then loads the URLs in the Story.chapterurls array in turn. For a long time I haven't figured out why it's sequential, because each reduce's function itself constructs a sequential structure of first-next, and promise then guarantees the order of the Code, That is, reduce puts multiple then strings together to generate a long list of then.
Getjson (' Story.json '). Then (function(story) {addhtmltopage (story.heading); returnStory.chapterUrls.reduce (function(chain, Chapterurl) {Console.log (' Reduce ', chain, chapterurl); //Once the last chapter ' s promise was done ... returnChain.then (function() { //... fetch the next chapter returnGetjson (Chapterurl); }). Then (function(chapter) {//and add it to the pageaddhtmltopage (chapter.html); }); }, Promise.resolve ()); }). Then (function() { //and we ' re all done!Addtexttopage ("All Done"); }).Catch(function(err) {//Catch Any error that happened along theAddtexttopage ("Argh, Broken:" +err.message); }). Then (function() { //Always hide the spinnerDocument.queryselector ('. Spinner '). Style.display = ' None '; });
Looking at the code of Bluebird in Friday, I want to find out how promise.promisify () can make the function of asynchronous callback into promise function. For a long while, it was found that the last parameter of the objective function was treated as callback, and the first parameter of the callback function was treated as err. There are pre-conditions, then it seems not as strong as I thought. Confused time went to see the next Bluebird MD document, the original people already said:
The node function should conform to node. JS Convention of accepting a callback as last argument and calling that callback With error as the first argument and success value is on the second argument.
Reduce + Promise Sequential execution code