async/await兑现承诺。仅当
async您要使用的函数
await返回Promise时,它们才起作用。
要解决您的问题,您可以使用类似的库,也可以
request-promise从
doRequest函数中返回promise 。
这是使用后者的解决方案。
function doRequest(options) { return new Promise ((resolve, reject) => { let req = http.request(options); req.on('response', res => { resolve(res); }); req.on('error', err => { reject(err); }); }); }describe('Requests', function() { var t = [ ]; t.forEach(function(options) { it('should return 200: ' + options.path, async function () { try { let res = await doRequest(options); chai.assert.equal(res.statusCode, 200); } catch (err) { console.log('some error occurred...'); } }); });});


