对 Requestify 的每次调用都允许您传递一个 Options 对象,该对象的定义在此处描述:Requestify API Reference
您正在使用 short 方法进行 POST,所以我将首先展示它,但同样的语法也适用于 put,请注意 get、delete、head 不接受一个数据参数,你通过params配置属性发送url查询参数。
requestify.post(url, data, config)
requestify.put(url, data, config)
requestify.get(url, config)
requestify.delete(url, config)
requestify.head(url, config)
现在,config 有一个 timeout 属性
超时 {number}
为请求设置超时(以毫秒为单位)。
因此我们可以使用以下语法指定 60 秒的超时时间:
var config = {};
config.timeout = 60000;
requestify.post(url, data, config)
或内联:
requestify.post(url, data, { timeout: 60000 })
现在让我们把它放在你的原始请求中:
正如@Jabalaja 指出的那样,您应该捕获任何异常消息,但是您应该在继续时使用错误参数来执行此操作。
(.then)
requestify.post('https://example.com/', {
email: 'foo@bar.com'
}, {
timeout: 60000
})
.then(function(response) {
var answer = response.getBody();
console.log("answer:" + answer);
}, function(error) {
var errorMessage = "Post Failed";
if(error.code && error.body)
errorMessage += " - " + error.code + ": " + error.body
console.log(errorMessage);
// dump the full object to see if you can formulate a better error message.
console.log(error);
});