【问题标题】:javascript promise inside object对象内部的javascript承诺
【发布时间】:2019-07-23 11:05:15
【问题描述】:

我可以在对象“addable:”中实现一个承诺。保存新客户端后,添加到 slimselect 的 id 需要等待新的 _id。 谢谢

支付至*
var Datastore = require('nedb');
var dclipro = new Datastore({filename: '/data/clipro.db', autoload: true});

var selectclipro = new SlimSelect({
                select: '#bcofid_clipro',
                valuesUseText: false,
                addable: function (value) {
                    var newid = "";

                    var agrabar = {
                          name: value,
                          last_idcuenta: "null",
                          email: "notdefined@gmail.com",
                          memo: "not available",
                          auditlog: moment().format('MMMM Do YYYY, h:mm:ss a')
                    }
                    dclipro.update({name: value}, agrabar, options, function (err, numReplaced, upsert) {

                        if (err) {
                            console.error(err);
                        }
                        newid = upsert._id;

                    })
                    iziToast.show({
                        title: 'Bank',
                        message: 'New Payee has been saved successfully',
                        position: 'topRight'
                    });
                    return { text: value, value: newid }

              },
              onChange: (info) => {
                console.log('elegido : value ' +info.value);
                console.log('elegido : text ' +info.text);
              }
            })

【问题讨论】:

  • 那么承诺在哪里?您是如何尝试使用的?
  • 很难解析出你在这里问什么。看起来您需要addable 才能返回对象{text: value, value: newid}。如果你不能从addable 返回一个 Promise,那将解决这个问题,那么不确定你是否可以,dclipro.update() 是你的异步请求(我猜)。由于代码现在可以工作,iziToast.show() 和返回都会在您的异步请求仍在触发时发生。在您确认没有错误之前,您不会希望显示成功(因此在 update 回调中),并且您不能返回您没有的东西。

标签: javascript node.js promise


【解决方案1】:

SlimSelect,哇,这东西过时了。从文档来看,您的return 对象似乎是可选的,并且您应该能够在您的承诺回调中执行类似selectclipro.setData(myNewValue) 的操作,只要addable 可以实际看到selectclipro。 (请确保您阅读了我上面的其余 cmets)

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2018-02-10
    相关资源
    最近更新 更多