using a fetch inside another fetch in javascript

后端 未结 3 636
青春惊慌失措
青春惊慌失措 2020-11-28 03:51

I want to get an api and after that call another one. Is it wisely using a code like this in javascript?

fetch(url, {
 method: \'get\',
 }).then(function(res         


        
3条回答
  •  一生所求
    2020-11-28 03:51

    This is a common question people get tripped up by when starting with Promises, myself included when I began. However, first...

    It's great you're trying to use the new Fetch API, but if I were you I would use a XMLHttpRequest implementation for now, like jQuery AJAX or Backbone's overridden implementation of jQuery's .ajax(), if you're already using these libraries. The reason is because the Fetch API is still so new, and therefore experimental at this stage.

    With that said, people definitely do use it, but I won't in my own production code until it's out of "experimental" status.

    If you decide to continue using fetch, there is a polyfill available. NOTE: you have to jump through extra hoops to get error handling to work properly, and to receive cookies from the server. If you're already loading jQuery, or using Backbone, just stick with those for now; not completely dreadful, anyway.

    Now onto code:

    You want a flat structure, else you're missing the point of Promises. It's not wise to nest promises, necessarily, because Promises solve what nested async callbacks (callback hell) could not.

    You will save yourself time and energy, and produce less buggy code by simply using a more readable code structure. It's not everything, but it's part of the game, so to speak.

    Promises are about making asynchronous code retain most of the lost properties of synchronous code such as flat indentation and one exception channel.

    -- Petka Antonov (Bluebird Promise Library)

    // run async #1
    asyncGetFn()
    // first 'then' - execute more async code as an arg, or just accept results
    // and do some other ops
    .then(response => {
        // ...operate on response data...or pass data onto next promise, if needed
    })
    // run async #2
    .then(asyncGetAnotherFn)
    .then(response => {
        // ...operate on response data...or pass data onto next promise, if needed
    })
    // flat promise chain, followed by 'catch'
    // this is sexy error handling for every 'then' above
    .catch(err => {  
      console.error('Request failed', err) 
      // ...raise exeption...
      // ... or, retry promise... 
    })
    

提交回复
热议问题