Javascript 无法从异步函数检索返回的对象
我正在努力将tweet函数的响应值记录到控制台中,但无论我做什么,即使tweet被发布,对象仍然返回空Javascript 无法从异步函数检索返回的对象,javascript,twitter,async-await,Javascript,Twitter,Async Await,我正在努力将tweet函数的响应值记录到控制台中,但无论我做什么,即使tweet被发布,对象仍然返回空 const Twitter = require('twitter'); const dotenv = require('dotenv'); dotenv.config(); const client = new Twitter({ consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST, consumer_secret: process
const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
let postRes = {};
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
})
.catch((error) => {
console.log('ERR');
throw error;
});
// console.log('POSTRES', postRes);
return postRes;
};
async function msg() {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
}
msg();
在这里,我期望语句
console.log('TWEETED',TWEETED)
返回一个包含两个元素的对象,tweet文本和发布tweet的id。但是,尽管将其包装在异步函数中,它返回空。尝试将tweet
函数转换为async
函数,如下所示,或者您可以从tweet
函数返回整个承诺本身
async function tweet(message, id = '0') {
let postRes = {};
let status = {};
let tweet;
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
try{
tweet = await client.post('statuses/update', status)
}
catch(error){
console.log('ERR: ', error)
throw error
}
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
};
async function msg() {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
}
msg();
归还全部承诺
const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
let postRes = {};
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
return client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
})
.catch((error) => {
console.log('ERR');
throw error;
});
// console.log('POSTRES', postRes);
// return postRes;
};
async function msg() {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
}
msg();
感谢Bergi指出范围问题。尝试将
tweet
函数转换为async
函数,如下所示,或者您可以从tweet
函数返回整个承诺本身
async function tweet(message, id = '0') {
let postRes = {};
let status = {};
let tweet;
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
try{
tweet = await client.post('statuses/update', status)
}
catch(error){
console.log('ERR: ', error)
throw error
}
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
};
async function msg() {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
}
msg();
归还全部承诺
const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
let postRes = {};
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
return client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
})
.catch((error) => {
console.log('ERR');
throw error;
});
// console.log('POSTRES', postRes);
// return postRes;
};
async function msg() {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
}
msg();
谢谢Bergi指出了范围问题。嗯,我认为您在这方面做得对,但您需要在呼叫成功返回时解决承诺,如下所示:
const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
// no direct return value
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
return client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
// here we resolve with the successful promise to keep the chain intact
return Promise.resolve(postRes);
})
.catch((error) => {
console.log('ERR');
throw error;
});
};
async function msg() {
// to handle any thrown errors use a try/catch here
try {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
} catch(error) {
console.log(`Error during post: ${error}`);
}
}
msg();
希望这有帮助 嗯,我认为您在这方面做得对,但您需要在呼叫成功返回时解决承诺问题,如下所示:
const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
// no direct return value
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
return client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
// here we resolve with the successful promise to keep the chain intact
return Promise.resolve(postRes);
})
.catch((error) => {
console.log('ERR');
throw error;
});
};
async function msg() {
// to handle any thrown errors use a try/catch here
try {
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
} catch(error) {
console.log(`Error during post: ${error}`);
}
}
msg();
希望这有帮助
Async/await是es8javascript中承诺的语法糖,但有时在解决承诺时,它会变得不那么强大。最近,我花了一整天的时间试图适应它们
您必须在async
中包装每个尝试使用基于承诺的函数的函数。请查看下面的代码以使用。
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
let postRes = {};
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
})
.catch((error) => {
console.log('ERR');
throw error;
});
// console.log('POSTRES', postRes);
return postRes;
};
async function msg() {
try{
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
//it returns <Promise>
return tweeted;
}catch(error){
console.log('Something went wrong', error);
return;
}
}
//No need to call getTweeks
async function getTweets(){
try{
//Do what you want with this Object
const tweet = await msg();
}catch(error){
console.log('Something went wrong', error);
return;
}
}
const dotenv=require('dotenv');
dotenv.config();
const client=newtwitter({
消费者密钥:process.env.TWITTER\u消费者密钥测试,
消费者秘密:process.env.TWITTER消费者秘密测试,
访问令牌密钥:process.env.TWITTER访问令牌密钥测试,
访问令牌密钥:process.env.TWITTER访问令牌密钥测试
});
常量tweet=(消息,id='0')=>{
设postRes={};
让状态={};
如果(id&&id.length>4){
状态={
在对状态的回复中,id:id,
状态:消息,
};
}否则{
状态={
状态:消息,
};
}
client.post('状态/更新',状态)
.然后((推特,回应)=>{
console.log('id',tweet.id);//tweet正文。
console.log('id_str',tweet.id_str);//tweet正文。
console.log('text',tweet.text);//tweet正文。
postRes.tweet=tweet.text,
postRes.id=tweet.id_str;
回帖;
})
.catch((错误)=>{
console.log('ERR');
投掷误差;
});
//console.log('POSTRES',POSTRES);
回帖;
};
异步函数msg(){
试一试{
const tweeted=wait tweet('这是异步的','');
console.log('TWEETED',TWEETED);
console.log('MESSAGE',tweeted.tweet);
console.log('ID',tweeted.ID);
//它回来了
返回推特;
}捕获(错误){
log('发生了错误',错误);
回来
}
}
//几周内不需要打电话
异步函数getTweets(){
试一试{
//对该对象执行所需操作
const tweet=等待消息();
}捕获(错误){
log('发生了错误',错误);
回来
}
}
我想这对你有帮助
Async/await是es8javascript中承诺的语法糖,但有时在解决承诺时,它会变得不那么强大。最近,我花了一整天的时间试图适应它们
您必须在async
中包装每个尝试使用基于承诺的函数的函数。请查看下面的代码以使用。
const dotenv = require('dotenv');
dotenv.config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});
const tweet = (message, id = '0') => {
let postRes = {};
let status = {};
if(id && id.length > 4) {
status = {
in_reply_to_status_id: id,
status: message,
};
} else {
status = {
status: message,
};
}
client.post('statuses/update', status)
.then((tweet, response) => {
console.log('id', tweet.id); // Tweet body.
console.log('id_str', tweet.id_str); // Tweet body.
console.log('text', tweet.text); // Tweet body.
postRes.tweet = tweet.text,
postRes.id = tweet.id_str;
return postRes;
})
.catch((error) => {
console.log('ERR');
throw error;
});
// console.log('POSTRES', postRes);
return postRes;
};
async function msg() {
try{
const tweeted = await tweet('this is_a__posts_async', '');
console.log('TWEETED', tweeted);
console.log('MESSAGE', tweeted.tweet);
console.log('ID', tweeted.id);
//it returns <Promise>
return tweeted;
}catch(error){
console.log('Something went wrong', error);
return;
}
}
//No need to call getTweeks
async function getTweets(){
try{
//Do what you want with this Object
const tweet = await msg();
}catch(error){
console.log('Something went wrong', error);
return;
}
}
const dotenv=require('dotenv');
dotenv.config();
const client=newtwitter({
消费者密钥:process.env.TWITTER\u消费者密钥测试,
消费者秘密:process.env.TWITTER消费者秘密测试,
访问令牌密钥:process.env.TWITTER访问令牌密钥测试,
访问令牌密钥:process.env.TWITTER访问令牌密钥测试
});
常量tweet=(消息,id='0')=>{
设postRes={};
让状态={};
如果(id&&id.length>4){
状态={
在对状态的回复中,id:id,
状态:消息,
};
}否则{
状态={
状态:消息,
};
}
client.post('状态/更新',状态)
.然后((推特,回应)=>{
console.log('id',tweet.id);//tweet正文。
console.log('id_str',tweet.id_str);//tweet正文。
console.log('text',tweet.text);//tweet正文。
postRes.tweet=tweet.text,
postRes.id=tweet.id_str;
回帖;
})
.catch((错误)=>{
console.log('ERR');
投掷误差;
});
//console.log('POSTRES',POSTRES);
回帖;
};
异步函数msg(){
试一试{
const tweeted=wait tweet('这是异步的','');
console.log('TWEETED',TWEETED);
console.log('MESSAGE',tweeted.tweet);
console.log('ID',tweeted.ID);
//它回来了
返回推特;
}捕获(错误){
log('发生了错误',错误);
回来
}
}
//几周内不需要打电话
异步函数getTweets(){
试一试{
//对该对象执行所需操作
const tweet=等待消息();
}捕获(错误){
log('发生了错误',错误);
回来
}
}
我想这会对您有所帮助。我没有彻底检查它,但您不应该也等待对msg()的调用吗?在我看来,这个程序似乎在async msg()结束之前就完成了,尽管我不知道它是用什么语言编写的。代码是用Javascript编写的。而且
msg()
运行正常,因为推特确实被发布了。好吧,我不知道Javascript有async/await。遗憾的是,它没有帮助,但因为我不确定,我把它作为一个注释而不是一个答案。你需要返回承诺,你的then()
creates没有彻底检查它,但是你不应该也等待调用msg()?在我看来,这个程序似乎在async msg()之前完成