javascript - 我将如何导出已解决的 promise

标签 javascript typescript ecmascript-6 es6-promise

我有很多 promise ,我需要导出其结果值。该值实际上是与嵌入式数据库的连接。

问题是我有几个中间件 promise 在解析最终数据库资源之前处理连接。我为数据库中的每个集合执行此操作。

如果我多次使用该方法,我将使用它自己的中间件的许多副本创建到该数据库的多个连接,这些中间件可能会变得相当大。我如何将这个 promise 和所有其他 promise 导出到一个最终对象中?

我应该创建一个单例类,在构造函数中解决 promise 并将集合项作为公共(public)变量吗?或者我应该如何解决将它们导出到对象中?

编辑 - 显示示例 promise 。众多之一

这只是集合连接之一。请注意,调用此方法时它会创建一个 new 集合。使用 IndexLoader 为每个集合创建内存中的二叉树。

这就是为什么我希望解析此方法并返回将作为集合的结果放入要在那里使用的对象中的原因。如果我要在整个应用程序中调用此方法,那么每次都会创建一个新的二叉树。

export const Users = (): Promise<Collection> => {
  return new Promise<Collection>((resolve, reject) => {
      const User: Collection = new Collection("Users");
      const indexPromises: Array<Promise<any>> = [];
      const uniqIndices: string[] = ["username"];

      User.pre("save", setDefaults);
      User.pre("update", updateDefaults);

      uniqIndices.forEach((v) => {
        indexPromises.push(IndexLoader(User, {fieldName: v, unique: true}));
      });

      Promise.all(indexPromises)
          .then(() => {
              resolve(User);
          })
          .catch(reject);
  });
};

编辑 2 - 我所说的单例解析导出是什么意思

我觉得这样使用单例是合理的。

import {Users} from "./file"; // this is the method above
export interface IDB {
    Users: Collection;
}
export class DB implements IDB {
    public static getInstance() {
        if (!DB.instance) {
            DB.instance = new DB();
        }
        return DB.instance;
    }
    private static instance: DB;
    public Users: Collection;
    
    constructor() {
        Users()
          .then((res) => {
              this.Users = res;
          })
          .catch(/** error handler */);
     }
}

使用其他文件中的类与相同的连接和二叉树进行交互

import {DB} from "..";
let db = DB.getInstance();
db.Users.insert({/** new object */})
    .then()
    .catch()

!!单例已尝试但不起作用!!

最佳答案

如果我理解你的话,结果包含在 promise 中。因此,您应该简单地返回包含打开的连接的对象,然后将其包装在已解决的 promise (then) 中,您应该能够在 then 中执行操作。

这是我拼凑的粗略 javascript 示例。

var p1 = new Promise((res, rej) => { 
   res( something_to_get_my_connection(input) ); //resolve the promise with the connection
}).then( (con) => {
   //Use connection. 
});

如果你的意思是你想要它在 promise 结构之外,我以前见过这个:

let myEstablishedConnection = null;
var p2 = new Promise((res, rej) => { 
   res( something_to_get_my_connection(input) ); //resolve the promise with the connection
}).then( (con) => {
   //Use connection. 
   myEstablishedConnection = con;
   continueProgram(); //This can be several things but I'm assuming the remaidner of the implementation is here. 
});

并且没有通常不受欢迎的外部范围变量:

var p3 = new Promise((res, rej) => { 
   res( something_to_get_my_connection(input) ); //resolve the promise with the connection
}).then( (con) => {
   continueProgram(con); //This can be several things but I'm assuming the remainder of the implementation is started here. 
});

要等待所有的 promise ,您可以使用以下内容:

Promise.all([p1,p2, p3]).then( () => { proceed(); } ); 

(这些例子不在我的脑海中,如果有错字或不一致,我深表歉意。promise.all 的用法显然是人为的。)

关于javascript - 我将如何导出已解决的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45150963/

相关文章:

javascript - 如何在 React Native 中使用 axios 将图像上传到服务器?

JavaScript 循环不会分隔 <TD> 元素

Typescript:如何根据数据结构生成和打印 AST

javascript - 在 ES6 类中停止 setInterval

Ace 文本编辑器的 TypeScript 声明

javascript - 我如何将接口(interface)应用于 typescript 中的匿名回调函数

javascript - 以干净的方式从构造函数参数为类设置属性

javascript - ruby rails : Get json from controller in Javascript file

javascript - requestAnimationFrame 实现是递归的吗?

javascript - 使用innerHTML和vanilla JS检索节点的内容