Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取带有等待的非阻塞函数的返回值_Javascript_Callback - Fatal编程技术网

Javascript 如何获取带有等待的非阻塞函数的返回值

Javascript 如何获取带有等待的非阻塞函数的返回值,javascript,callback,Javascript,Callback,假设我有以下功能: async function commentMediaId(ig, media_id, commentContent, commentIfAlreadyCommented = false, extraInfo = new Object(), callback) { try { let alreadyExists = ig.db.get('comments').find({media_id: media_id}).value(); al

假设我有以下功能:

async function commentMediaId(ig, media_id, commentContent, commentIfAlreadyCommented = false, extraInfo = new Object(), callback) {

    try {
        let alreadyExists = ig.db.get('comments').find({media_id: media_id}).value();
        alreadyExists == undefined ? false : true;
        if(alreadyExists && !commentIfAlreadyCommented) {
            console.log('Already commented'.yellow);
            return "already_commented";
        }

        if(commentIfAlreadyCommented || !alreadyExists){
            await ig.media.comment({
                module_name: 'profile',
                mediaId: media_id,
                text: commentContent,
            });


            return callback('success');
        }
    } catch (e) {
        return callback('not success');
    }

}
commentMediaId(object, 'someid', 'some string') 
  .then(function (result) {
    console.log(result);
  })
;
然后我调用以下函数(我不想在这里使用wait,因为它会阻塞):

我如何在commentMediaId上看到回调,以便在它返回时获得成功或不成功的值

我基本上想做如下事情

commentMediaId(object, 'someid', 'some string', function(result) {
   if (result == 'success')



}); 

但是我得到了一大堆语法错误。。关于如何实现它有什么想法吗?

当您使用
异步
函数时,您使用的是
Promise
接口,该接口公开了
.then()
方法。您可以将回调传递到此方法中,当从
commentMediaId
函数返回值时,将自动调用回调:

async function commentMediaId(ig, media_id, commentContent, commentIfAlreadyCommented = false, extraInfo = new Object(), callback) {

    try {
        let alreadyExists = ig.db.get('comments').find({media_id: media_id}).value();
        alreadyExists == undefined ? false : true;
        if(alreadyExists && !commentIfAlreadyCommented) {
            console.log('Already commented'.yellow);
            return "already_commented";
        }

        if(commentIfAlreadyCommented || !alreadyExists){
            await ig.media.comment({
                module_name: 'profile',
                mediaId: media_id,
                text: commentContent,
            });


            return callback('success');
        }
    } catch (e) {
        return callback('not success');
    }

}
commentMediaId(object, 'someid', 'some string') 
  .then(function (result) {
    console.log(result);
  })
;
最后,您不需要在
commentMediaId
函数中接受
callback
参数。您可以省略
回调
,直接返回值。这就是async/await“语法糖”的全部思想,它使它感觉更像一个常规函数,而不需要回调

例如:

return callback('success');
return 'success';
…应更改为以下内容:

return callback('success');
return 'success';

看起来您有一些输入错误-
asyn
应该是
async
,并且在函数
commentMediaId(…otherArgs,callback(){}
的最后一个参数中还有一个额外的圆括号,而不是
commentMediaId(…otherArgs,callback){}
callback()
是一种语法error@dandavis我已经编辑了一些问题中的错误。如果您正在使用promises和
async
/
Wait
,您不应该通过回调。我认为这些只是问题中的错误。我已经编辑了OP的问题来修复它们。问题本身是另一个问题。