2017-10-06 3 views
0

を解決できない約束、約束解決:約束行を作成しますがseqalizerjsを使用して

return user.save(data).should.eventually.equal('123123'); 

としてチャイと

return new Promise(function(resolve, reject){ 
      return models.merchants.create({ 
       name: ctx.name, 
      }); 
     }).then(function(result){ 
      resolve({ 
       id: result.id 
      }); 
     }).catch(function(err){ 
      reject(err); 
     }); 

テストが、私はいつもこの取得:私は考えて

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure 
it resolves. 

答えて

0

を問題は次の行にあります。

return new Promise(function(resolve, reject){ 
     return models.merchants.create({ 
      name: ctx.name, 
     }); 
     // ..... 

Promise関数resolve()を有しているべきである、このような何か:this referenceに基づい

return new Promise(function(resolve, reject){ 
     resolve(models.merchants.create({ 
      name: ctx.name, 
     })); 

​​

はまだ基準に基づいて、Promiseフローグラフがある。 enter image description here

then()またはcatch()Promise()の後にresolve()reject()も処理されない場合、渡される値はresolve()またはreject()のいずれかにしか処理されません。

だから、私はあなたのコードは次のようなものであるべきだと思う:あなたのコードは次のようになります

return new Promise(function(resolve, reject){ 
    resolve(models.merchants.create({ 
     name: ctx.name, 
    })); 
}).then(function(merchants){ 
    console.log(merchants) // the created `merchants` from the `resolve()` of the promise would be passed here. 
    return merchants; 
}); 

UPDATE

return new Promise(function(resolve, reject){ 
    models.merchants.create({ // assuming `models.merchants.create()` is an async method 
     name: ctx.name, 
    }).then(function(result){ 
     resolve({ 
      id: result.id 
     }); 
    }).catch(function(err){ 
     reject(err); 
}); 
+0

が、今の問題はにReferenceError次のとおりです。 rejectは定義されていません – Alvin

+0

あなたは 'catch()'の 'reject()'も削除してください。 'Promise()'で 'reject()'を使うべきです – samAlvin

+0

@Alvin私は自分の答えを更新しました:) – samAlvin

関連する問題