.on事件获取值后返回承诺

问题描述

我正在使用ml5.js PoseNet。我有一个获取图像的函数,并且试图返回PoseNet生成的单个姿势。我也想返回此值作为承诺。使用下面的toReturn,它会在姿势完成生成之前返回,因此是不确定的(因为我想是异步特性)。我该如何解决?

async function addPose(img){
    let options = {
        imageScaleFactor: 1,minConfidence: 0.1
    }

    let poseNet = ml5.poseNet(modelReady,options);

    function modelReady(){
        console.log('model loaded');
        let result = poseNet.singlePose(img);    // a promise of JSON value
        // return result;
    }

    let toReturn;

    poseNet.on('pose',function(results){
        toReturn = results[0]     // the JSON value
        // return results[0]
    })
    return toReturn;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)