0

我无法用这个脚本解决我未处理的承诺拒绝。承诺和异步功能对我来说都是新的。我应该处理拒绝,但不知道它是如何工作的。

有人能帮我吗?

const rawRequest = async (url, headers, data, timeout) => {
// Set custom User-Agent string
headers['User-Agent'] = 'Kraken Javascript API Client';

const options = { headers, timeout };

Object.assign(options, {
    method : 'POST',
    body   : qs.stringify(data),
});

const { body } = await got(url, options);
reject('reject');

const response = JSON.parse(body);

if(response.error && response.error.length) {
    const error = response.error
        .filter((e) => e.startsWith('E'))
        .map((e) => e.substr(1));

    if(!error.length) {
        throw new Error("Kraken API returned an unknown error");
    }

    throw new Error(error.join(', '));
}

return response;
};

//script
const response = rawRequest(url, headers, params, timeout);
console.log(response);
4

1 回答 1

0

async 函数的结果在你的 then. 像这样调用你的 rawRequest 函数(不要做 response = rawRequest()):

//script
rawRequest(url, headers, params, timeout).then(res => {
//do something with your response
 console.log(res);
}).catch(e => {
//handle exception (rejection)
console.log(e);
})

希望这可以帮助!!!

于 2017-08-13T21:22:02.933 回答