如何检索函数返回的值?

How to retrieve value returned from function?

我有 model.scan().exec() 函数将数据对象传递给处理函数。我想将返回值分配给来自外部上下文的变量。我该怎么做?

function handler (err, data) {
    return data.Count;
}


await model.scan().exec(handler);
let count = // i want it to be data.Count from handler

我真的不知道 model.scan().exec() 是做什么的,但你可以这样做:

await model.scan().exec(() => {
 const result = handler.apply(null, arguments);
});

await model.scan().exec((err, data) => {
 const result = handler(err, data);
});

试试这个代码

 function handler (model) {
        return new Promise((resolve, reject) => 
          model.scan().exec((err, data) => err? reject(err) : resolve(data.Count))
        )
 }


 const count = await handler(model);