Use Promise to wait until polled condition is satisfied

后端 未结 5 575
孤独总比滥情好
孤独总比滥情好 2020-12-23 09:34

I need to create a JavaScript Promise that will not resolve until a specific condition is true. Let\'s say I have a 3rd party library, and I need to wait until a certain da

5条回答
  •  生来不讨喜
    2020-12-23 09:46

    function getReportURL(reportID) {
      return () => viewReportsStatus(reportID)
      .then(res => JSON.parse(res.body).d.url);
    }
    
    function pollForUrl(pollFnThatReturnsAPromise, target) {
      if (target) return P.resolve(target);
      return pollFnThatReturnsAPromise().then(someOrNone => pollForUrl(pollFnThatReturnsAPromise, someOrNone));
    }
    
    pollForUrl(getReportURL(id), null);
    

提交回复
热议问题