最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Promise reject not working inside of callback - Stack Overflow

programmeradmin1浏览0评论

I'm writing a module that uses the Google API, but am wrapping everything that is callback based in a promise. This is the code of the problem area

file1.js

var File2 = require('file2')
var api = new File2()
api.auth().then(auth => {
  api.search('example').then(res => {
     ...do some stuff...
  })
}).catch(err => {
  console.log('1') //Not being run
  throw err
})

file2.js

class File2(){
  auth() {
    ...works fine and resolves...
  }

  search() {
     return new Promise((resolve, reject) => {
       googleapi.somemethod(options, (err, res) => {
         if(err) { 
           console.log('2') // DOES run
           reject(new Error(err))
         }
         resolve(res.field) //Program crashes here because reject didn't actually reject
       })
     })

}

The call to auth is working just fine, but the call to search (and more specifically googleapi.somemethod) is failing, and err is defined. I check for err, and console.log('2') runs, but then console.log('1') in catch doesn't run, the error isn't thrown, and the program crashed on resolve(res) because res is undefined. I've tried putting the error catcher as the second argument to then instead of using catch, but that still doesn't work

api.search('example').then(res => {
  ...do some stuff...
 }, err => {
    console.log('2') // Still doesn't run
    throw err
 })

I'm running Node v6.2.1

I'm writing a module that uses the Google API, but am wrapping everything that is callback based in a promise. This is the code of the problem area

file1.js

var File2 = require('file2')
var api = new File2()
api.auth().then(auth => {
  api.search('example').then(res => {
     ...do some stuff...
  })
}).catch(err => {
  console.log('1') //Not being run
  throw err
})

file2.js

class File2(){
  auth() {
    ...works fine and resolves...
  }

  search() {
     return new Promise((resolve, reject) => {
       googleapi.somemethod(options, (err, res) => {
         if(err) { 
           console.log('2') // DOES run
           reject(new Error(err))
         }
         resolve(res.field) //Program crashes here because reject didn't actually reject
       })
     })

}

The call to auth is working just fine, but the call to search (and more specifically googleapi.somemethod) is failing, and err is defined. I check for err, and console.log('2') runs, but then console.log('1') in catch doesn't run, the error isn't thrown, and the program crashed on resolve(res) because res is undefined. I've tried putting the error catcher as the second argument to then instead of using catch, but that still doesn't work

api.search('example').then(res => {
  ...do some stuff...
 }, err => {
    console.log('2') // Still doesn't run
    throw err
 })

I'm running Node v6.2.1

Share Improve this question asked Jun 14, 2016 at 7:05 WestonWeston 1,4613 gold badges13 silver badges26 bronze badges 3
  • Shouldn't you return the promise? Also shouldn't there be an else? It looks like resolve will always be called regardless... – elclanrs Commented Jun 14, 2016 at 7:10
  • Am I not returning the promise? I've never seen someone do anything like > return resolve() or >return reject() And as for the else, you might be right. I'm following the mon pattern of handling callbacks that return something, but yeah this might not work for promises. I've always had it in my head though that "resolve" and "reject" sort of act like a return, but that may just be a hole in my knowledge of promises. – Weston Commented Jun 14, 2016 at 7:12
  • Check my answer, hope that helps. Also, nevermind about the else part. – elclanrs Commented Jun 14, 2016 at 7:20
Add a ment  | 

2 Answers 2

Reset to default 6

You should return the promise:

var File2 = require('file2')
var api = new File2()
api.auth().then(auth => {
  return api.search('example').then(res => { // return the promise
     return ...
  })
}).catch(err => {
  console.log('1') // Not being run
  throw err
})

Also, if you don't need auth inside search then you can unnest those promises:

var File2 = require('file2')
var api = new File2()
api.auth().then(auth => {
  return api.search('example')
}).then(res => {
   return ...
}).catch(err => {
  console.log('1') //Not being run
  throw err
})

calling reject() does not stop your program, all codes below will be executed too.

Please update from

if(err) { 
  console.log('2') // DOES run
  reject(new Error(err))
}
resolve(res.field) //Program crashes here because reject didn't actually reject

to

if(err) { 
  console.log('2') // DOES run
  reject(new Error(err))
}
else {
  resolve(res.field) //Program crashes here because reject didn't actually reject
}

* update * or you can shorten your code to

if(err) { 
  console.log('2') // DOES run
  return reject(err) // no need to new Error object
}
resolve(res.field) //Program crashes here because reject didn't actually reject
发布评论

评论列表(0)

  1. 暂无评论