如何将已解决的承诺值传递给另一个函数?
How to pass a resolved promise value to another function?
如何将返回的 Promise
值传递给另一个函数?我需要检索已解析的 JSON
数据,然后 使用该响应数据 发送到 Slack 频道。
我的index.js
// Get the data
let getData = () => {
axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const responseJSON = res.data
return responseJSON
})
.catch(err => {
console.log(`Error in getData(): ${err}`)
})
}
// Post to Slack (real-time)
let slack = () => {
axios.post('url-to-post-to', {
'text': getData().toString() // This has to be a string, but right now it's returning [object Promise]
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
})
}
现在我在 Slack 频道中收到 [object Promise]
,但我需要以 string.[= 形式返回的 JSON
15=]
我认为我正在尝试传递一个尚未解析的值,但问题是,我不知道解析后如何传递该值。
感谢任何帮助。
您可以将函数包装在 async-await
中。
let slack = async () => {
axios.post('url-to-post-to', {
'text': await getData().toString()
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
})
}
在第一次通话后插入您的 Slack 通话 thenabled。您需要这样做,因为正如您所怀疑的那样,您正在执行 Slack 调用,结果是未实现的承诺。
// Get the data
let getData = () => {
axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const respondeJSON = res.data;
return responseJSON;
}).then(data => {
axios.post('url-to-post-to', {
'text': data
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`);
});
}).catch(err => {
console.log(`Error in getData(): ${err}`)
})
};
使用 promise 时,如果函数返回了 promise,您可以对其调用 then 属性,解析后的值将通过 then 参数传递。你可以的。
// Get the data
let getData = () => {
return axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const respondeJSON = res.data
return responseJSON
})
.catch(err => {
console.log(`Error in getData(): ${err}`)
})
}
// Post to Slack (real-time)
let slack = () => {
getData().then(data =>
axios.post('url-to-post-to', {
'text': data.toString()
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
}))
}
或
// Get the data
let getData = () => {
return axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const respondeJSON = res.data
return responseJSON
})
.catch(err => {
console.log(`Error in getData(): ${err}`)
})
}
// Post to Slack (real-time)
let slack = (data) => {
axios.post('url-to-post-to', {
'text': data.toString()
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
}))
}
getData().then(slack);
如何将返回的 Promise
值传递给另一个函数?我需要检索已解析的 JSON
数据,然后 使用该响应数据 发送到 Slack 频道。
我的index.js
// Get the data
let getData = () => {
axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const responseJSON = res.data
return responseJSON
})
.catch(err => {
console.log(`Error in getData(): ${err}`)
})
}
// Post to Slack (real-time)
let slack = () => {
axios.post('url-to-post-to', {
'text': getData().toString() // This has to be a string, but right now it's returning [object Promise]
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
})
}
现在我在 Slack 频道中收到 [object Promise]
,但我需要以 string.[= 形式返回的 JSON
15=]
我认为我正在尝试传递一个尚未解析的值,但问题是,我不知道解析后如何传递该值。
感谢任何帮助。
您可以将函数包装在 async-await
中。
let slack = async () => {
axios.post('url-to-post-to', {
'text': await getData().toString()
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
})
}
在第一次通话后插入您的 Slack 通话 thenabled。您需要这样做,因为正如您所怀疑的那样,您正在执行 Slack 调用,结果是未实现的承诺。
// Get the data
let getData = () => {
axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const respondeJSON = res.data;
return responseJSON;
}).then(data => {
axios.post('url-to-post-to', {
'text': data
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`);
});
}).catch(err => {
console.log(`Error in getData(): ${err}`)
})
};
使用 promise 时,如果函数返回了 promise,您可以对其调用 then 属性,解析后的值将通过 then 参数传递。你可以的。
// Get the data
let getData = () => {
return axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const respondeJSON = res.data
return responseJSON
})
.catch(err => {
console.log(`Error in getData(): ${err}`)
})
}
// Post to Slack (real-time)
let slack = () => {
getData().then(data =>
axios.post('url-to-post-to', {
'text': data.toString()
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
}))
}
或
// Get the data
let getData = () => {
return axios.get('https://jsonplaceholder.typicode.com/posts/1').then(res => {
const respondeJSON = res.data
return responseJSON
})
.catch(err => {
console.log(`Error in getData(): ${err}`)
})
}
// Post to Slack (real-time)
let slack = (data) => {
axios.post('url-to-post-to', {
'text': data.toString()
})
.catch(err => {
console.error(`Error in SLACK: ${err.response.data}`)
}))
}
getData().then(slack);