Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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 使用async/await进行sequelize查询的无限循环_Javascript_Node.js_Orm_Async Await_Sequelize.js - Fatal编程技术网

Javascript 使用async/await进行sequelize查询的无限循环

Javascript 使用async/await进行sequelize查询的无限循环,javascript,node.js,orm,async-await,sequelize.js,Javascript,Node.js,Orm,Async Await,Sequelize.js,我想调用一个函数,它在一个无限循环中包含一个sequelize查询(返回一个顺序),这意味着在解析函数时再次调用该函数,因此始终搜索新的顺序 我最初的方法是 do { (async function () { var nextOrder = await getNextOrder() console.log("nextOrder",nextOrder) })(); } while (ENV == "development"); 这就要求: fun

我想调用一个函数,它在一个无限循环中包含一个sequelize查询(返回一个顺序),这意味着在解析函数时再次调用该函数,因此始终搜索新的顺序

我最初的方法是

do {
    (async function () {
        var nextOrder = await getNextOrder()
        console.log("nextOrder",nextOrder)
    })(); 
} while (ENV == "development");
这就要求:

function getNextOrder() {
    return new Promise(async (resolve, reject) => {
        try {
            Orders.findOne({
                where: {
                    type: {
                        [Op.or]: ["buy", "sell"]
                    },
                },
            }).then(async nextOrder => {
                // Doing stuff with next order
                return resolve()
            }).catch(function (e) {
                console.log("error", e)
                return reject(e)
            })
        } catch (e) {
            console.log("error", e)
            return reject(e)
        }
    })
}
function getNextOrder() {
    Orders.findOne({
        where: {
            type: {
                [Op.or]: ["buy", "sell"]
            },
        },
    }).then(async nextOrder => {
        // Doing stuff with next order
    }).then(function() {
        getNextOrder() // <------ Calling function again here
        return
    }).catch(function (e) {
        console.log("error", e)
        return
    })
}
上述方法的问题是:getNextOrder函数永远不会解析,而while循环只是永久调用它。通过一次又一次地调用同一个函数,但在问题解决后,需要做些什么才能使上述代码正常工作

现在我使用了下面的方法,这是可行的,但是让上面的异步/等待方法发挥作用会很有趣

getNextOrder()
这就要求:

function getNextOrder() {
    return new Promise(async (resolve, reject) => {
        try {
            Orders.findOne({
                where: {
                    type: {
                        [Op.or]: ["buy", "sell"]
                    },
                },
            }).then(async nextOrder => {
                // Doing stuff with next order
                return resolve()
            }).catch(function (e) {
                console.log("error", e)
                return reject(e)
            })
        } catch (e) {
            console.log("error", e)
            return reject(e)
        }
    })
}
function getNextOrder() {
    Orders.findOne({
        where: {
            type: {
                [Op.or]: ["buy", "sell"]
            },
        },
    }).then(async nextOrder => {
        // Doing stuff with next order
    }).then(function() {
        getNextOrder() // <------ Calling function again here
        return
    }).catch(function (e) {
        console.log("error", e)
        return
    })
}
函数getNextOrder(){ 命令,芬顿({ 其中:{ 类型:{ [作品或]:[“买”、“卖”] }, }, }).then(异步下一步=>{ //做下一个订单的工作 }).然后(函数(){
getNextOrder()//您不应在
新承诺(异步(解析、拒绝)
代码段中使用
async
,或在函数内部使用
async/await

function getNextOrder() {
    return new Promise(async (resolve, reject) => {
        try {
            const nextOrder = await Orders.findOne({
                where: {
                    type: {
                        [Op.or]: ["buy", "sell"]
                    },
                },
            })
            // Doing stuff with next order
            return resolve()
        } catch (e) {
            console.log("error", e)
            return reject(e)
        }
    })
}