如何在promise中获得回调响应

时间:2018-03-29 00:06:48

标签: javascript es6-promise

我在promise中使用promise并调用外部函数来获取数据。外部函数使用回调来发回数据。有人可以帮助如何发送回应响应承诺?下面是我的代码

function processData( data ){
....
}

function getData( data ) {

   const callbacks = {
     error(e) {
      console.log('error',e)
     }
     dataReady(data){
      // I want this data to be sent back to my original promise, while which use in processData function
      return data
    }
  }
  const obj = api.init( args, callbacks);

 // TODO: 1) How can I wait this function to complete the callbacks
           2) How can I capture callbacks response 

}


new Promise( ( res, rej ) => {
const req = new Request( args );
req.send()
  .then( getData )
  .then( processData )
  .then( ( data ) => res( data ) )
  .catch( ( err ) => rej( err ) );
} );

0 个答案:

没有答案