从Dexie的承诺中获取价值

时间:2017-09-15 00:34:06

标签: javascript promise dexie

尝试使用Dexie.JS

获取以下函数返回的数据
function findEvent(key) {
  let snippet = '';
  if (!db.isOpen()) {
    lastEvent = lastEvent.then(() => db.open());
  }
  lastEvent = lastEvent.then(() => db.transaction('r', db.events, () => {
    db.events.each((element) => {
      let d = element.cause.data;
      if (d.hasOwnProperty('deleted') && (false == d.deleted) && d.hasOwnProperty('abbreviation') && 
        d.hasOwnProperty('contents') && (d.abbreviation == key)) {
          console.log(`found matching snippet for key: ${key}, contents: ${d.contents}`);
          snippet = d.contents;
      }
    }).then(() => {
      console.log('search callback returning:', snippet);
      return snippet;
    });
  }));

  return lastEvent;
}

我称之为:

  return findEvent(request.key).then((result) => {
    console.log(`findEvent return: ${result}`);
  });

显然,我做错了什么。我的承诺以正确的顺序执行。我从snippet内部的日志中正确打印findEvent。在调用代码中记录result时,它为undefined

应该采取哪些不同的方式?

1 个答案:

答案 0 :(得分:1)

你不是{{1}来自chmod +r term_boot.exp的承诺,所以未定义会被传达到你的顶级承诺中。