将Sequelize承诺嵌入javascript异步/等待链

将Sequelize承诺嵌入javascript异步/等待链,javascript,node.js,promise,async-await,sequelize.js,Javascript,Node.js,Promise,Async Await,Sequelize.js,我试图在现有的异步/等待链中使用Sequelize承诺。Sequelize以承诺的形式返回所有内容。我不熟悉承诺和异步/等待 所以我把这当作一种学习经验。请对我格外耐心 我的逻辑是这样的: yourTable.findAll({ /* ... */ }) .then((data) => { if (!Array.isArray(data) || !data.length) { // We need to create one, return

我试图在现有的异步/等待链中使用Sequelize承诺。Sequelize以承诺的形式返回所有内容。我不熟悉承诺和异步/等待 所以我把这当作一种学习经验。请对我格外耐心

我的逻辑是这样的:

yourTable.findAll({ /* ... */ })
    .then((data) => {
        if (!Array.isArray(data) || !data.length) {
            // We need to create one, return the new ID
            return myTable.create({ /* ... */ })
                .then((result) => result._id)
        } else {
            // We found one, return the existing ID
            return data[0]._id
        }
    })
如果上一个“.”中不存在行,则

创建新行并提取新id

否则

从上一个“.then”中提取id值

因此,通过学习许多优秀的So示例,我现在有了一个正在工作的代码块(如下所示)。我想使用所有匿名函数,因为IMHO使它更容易阅读。(是的,这一点值得商榷,但目前我想尝试使用匿名函数

有三个嵌套的返回语句似乎很奇怪。有没有办法重写这段代码,使其更干净一些,但只使用匿名函数

yourTable.findAll( {...})
.then( (data) => {
      // more code
})
.then( (data) => {
      // more code
})
.then( (data) => {
     if  ( !Array.isArray(data) || !data.length ) {  
           // if record does NOT exist
         return (async function ()       {
             return await
                 (function ()    {
                     return new Promise( resolve => {
                         myTable.create( { ........ }
                            ).then( (result) => {
                                resolve(result._id);
                        })
                    })
                })();
            })()    
            /* we had to create one, so we return the *NEW* _id value */
    } else {
            /* we found one, so we just return the pre-existing _id value */                                                                                      
        return  data[0]._id    ;
    }
})
.then( (data) => {
    // more code
})
.then( (data) => {
谢谢大家。

删除所有不必要的,以及:

删除所有不必要的、不必要的和:


承诺只是可以
等待
的值,但实际上并不需要严格地
等待
它们,特别是当您周围的结构没有使用
异步
/
等待
时。您可以这样编写:

yourTable.findAll({ /* ... */ })
    .then((data) => {
        if (!Array.isArray(data) || !data.length) {
            // We need to create one, return the new ID
            return myTable.create({ /* ... */ })
                .then((result) => result._id)
        } else {
            // We found one, return the existing ID
            return data[0]._id
        }
    })
如果您从
中返回承诺,那么()
将成为父链的一部分,这可能正是您想要的

如果您真的想使用
async
/
wait
,那么我建议在您的快乐之路得到解释后,翻转逻辑并进行更大的工作,如下所示:

yourTable.findAll({ /* ... */ })
    .then(async (data) => {
        if (data && data.length) return data[0]._id

        const result = await myTable.create({ /* ... */ })

        return result._id
    })

承诺只是可以
等待
的值,但实际上并不需要严格地
等待
它们,特别是当您周围的结构没有使用
异步
/
等待
时。您可以这样编写:

yourTable.findAll({ /* ... */ })
    .then((data) => {
        if (!Array.isArray(data) || !data.length) {
            // We need to create one, return the new ID
            return myTable.create({ /* ... */ })
                .then((result) => result._id)
        } else {
            // We found one, return the existing ID
            return data[0]._id
        }
    })
如果您从
中返回承诺,那么()
将成为父链的一部分,这可能正是您想要的

如果您真的想使用
async
/
wait
,那么我建议在您的快乐之路得到解释后,翻转逻辑并进行更大的工作,如下所示:

yourTable.findAll({ /* ... */ })
    .then(async (data) => {
        if (data && data.length) return data[0]._id

        const result = await myTable.create({ /* ... */ })

        return result._id
    })

这是我尝试的第一件事,我得到了回报:“SyntaxError:await仅在异步函数中有效”当然,您需要使用
async(data)=>{
回调你的
then
函数当然了。但是你真的不应该混合
then
await
语法,而是让外部函数也使用
async
/
await
语法,你能给我们看更多的代码吗?或者如果你不想使用
await
,它应该是一个简单的
返回myTable.create({…})。然后(result=>result.\u id);
if
块中。“而是生成外部函数”-好的,我注意到了…!我添加了更多的代码,但实际上没有太多其他内容。您可能应该从
const data=wait yourTable.findAll({…})开始;//更多代码
等。然后对于
if
/
else
,当您不能只
返回
时,分配给变量(或使用条件运算符)。将所有这些放在
异步
函数中(或者,如果它已经在函数中,则在该函数中添加
异步
关键字)。这是我尝试的第一件事,我得到了回报:“SyntaxError:await仅在异步函数中有效”当然,您需要使用
async(data)=>{
回调你的
then
函数当然了。但是你真的不应该混合
then
await
语法,而是让外部函数也使用
async
/
await
语法,你能给我们看更多的代码吗?或者如果你不想使用
await
,它应该是一个简单的
返回myTable.create({…})。然后(result=>result.\u id);
if
块中。“而是生成外部函数”-好的,我注意到了…!我添加了更多的代码,但实际上没有太多其他内容。您可能应该从
const data=wait yourTable.findAll({…})开始;//更多代码
等。然后对于
if
/
else
,当您不能只
返回
时,分配给变量(或使用条件运算符)。将所有这些放在
异步
函数中(或者,如果它已经在函数中,则在该函数中添加
异步
关键字).昨天****你在哪里?你本可以帮我省下几个小时无用的研究。但是,说真的,非常感谢你。我遵循的逻辑有点奇怪,因为我必须把其中一张表读两遍,但是你的第一个建议非常有效,使代码更具可读性。我很惊讶这样的情况不会ccur更频繁。你应该在某个地方发表你的优秀建议,而不仅仅是在这里。你昨天在哪里?你会帮我省下几个小时无用的研究。但说真的,非常感谢你。我遵循的逻辑有点奇怪,因为我不得不读两遍表中的一个,但是你的第一个建议奏效了非常完美,使代码更具可读性。我很惊讶这样的情况不会经常发生。您应该在某个地方发布您的优秀建议,而不仅仅是在这里。