node.js - promise嵌套的问题,后面一个then用到前面的then的返回值。

查看:243
本文介绍了node.js - promise嵌套的问题,后面一个then用到前面的then的返回值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

nodejs中关于ES6的promise嵌套写法
我打算实现如下功能:插入主记录,返回insertId,然后插入明细记录

testObject.insertMain(code,name)
.then((result)=>{
    var insertId = result.insertId;
    testObject.insertDetail(insertId,........)
    .then((result1)=>{
        testObject.func3(......)
        .then(..)
        .catch(..)
    })
    .catch(..)
.catch(..)

这种逻辑似乎又进入了无限嵌套逻辑。求教如何写正确!

解决方案

testObject.insertMain(code,name)
.then((result)=>{
    var insertId = result.insertId;
    return insertId;
})
.then(function(insertId){
    return testObject.insertDetail(insertId);
})
.then(function(detail){
    console.log(detail);
})
.catch(..)

这篇关于node.js - promise嵌套的问题,后面一个then用到前面的then的返回值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆