Reduce + Promise Sequential execution code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.