How to structure nested Promises

前端 未结 3 1094
误落风尘
误落风尘 2020-11-30 12:00

I have a situation where I think the only choice for me is to nest some Promises within each other. I have a Promise that needs to be performed and a method that does someth

3条回答
  •  不知归路
    2020-11-30 12:59

    you can use generator to flatten your nested promises (Bluebird.couroutine or Generators)

    //Bluebird.couroutine
    const generator = Promise.coroutine(function*() {
      try {
         const value = yield fetchValue(url);
         const success = yield saveToCache(value);
         console.log('success:', success);
      } catch(e) {
         console.error(err);
      }    
    }));
    
    generator();
    

提交回复
热议问题