错误:我一直收到“承诺是在处理程序中创建的,但未从中返回”

时间:2017-09-17 18:22:23

标签: javascript sharepoint promise bluebird es6-promise

我无法弄清楚为什么我可以压制这个警告

导致错误的方法

    CreateSettingsList.    init: function () {
  var collection = new $pnp.Web(config.settings.defaultCollection);

  return collection.lists.ensure(SPConfig.listName, SPConfig.listDescription, 100, false, {
      EnableVersioning: true,
      Hidden: true
    })
    .then(r => {
      // console.log("List created:", r.created);

      // IF SETTINGS LIST EXIST RETURN SETTINGS 
      if (!r.created) {
        //console.log('Config list exists');

        return collection.lists
          .getByTitle(SPConfig.listName).items
          .getById(1).select("Settings")
          .usingCaching().get()
          .then(item => {
            config = JSON.parse(item.Settings);
            SETTINGS_LOADED = true;
            return config;
          })
          .catch(console.log);

      } else {
        // CREATE CONFIG LIST - RETURN SETTINGS
        console.log('Creating list');

        // Variable setup 
        var batch = $pnp.sp.createBatch();
        var list = collection.lists.getByTitle(SPConfig.listName);

        // Add settings field 
        list.fields.inBatch(batch).addMultilineText(SPConfig.listField, 10, false, false, false, false, undefined)
          .then(_ => console.log("created field"))
          .catch(console.log);

        // Update View 
        list.views.getByTitle(SPConfig.listView)
          .fields.inBatch(batch).add(SPConfig.listField)
          .then(_ => console.log("update view"))
          .catch(console.log);

        // Add default settings
        list.items.inBatch(batch).add({
            Title: SPConfig.listTitle,
            Settings: JSON.stringify(config)
          })
          .then(_ => console.log("created item"))
          .catch(console.log);

        // Execute batch  
        return batch.execute()
          .then(_ => {
            SETTINGS_LOADED = true;
            return config;
          })
          .catch(e => {
            throw e;
          });
      }
    })
    .catch(console.log);
}

调用它时(SettingService):

  function loadSettings() {    
      // CHECK IF SETTINGS LIST EXISTS BEFORE GETTING ITEM
      return CreateSettingsList.init();
    }
  }

当被称为:

function getAccess() {
    if (!ACCESS_CHECKED) {
      SettingService.loadSettings().then(function (config) { settings = config; });
    }
}

我正在努力确保我已经回复过哪些东西,我已经做了一个承诺电话,我之前得到了更多这些,但我想我想出了如何解决它们但是这个不会消失。

1 个答案:

答案 0 :(得分:0)

else子句中,“添加设置字段”,“更新视图”和“添加默认设置”这三个部分均创建了一个承诺,其中任何一个都不会对CreateSettingsList.init()返回的承诺产生影响因此警告。

很难确切地说应该应用什么修正,但是一个选项是聚合这三个承诺,如果所有三个承诺都执行batch.execute()

CreateSettingsList.init: function() {
    var collection = new $pnp.Web(config.settings.defaultCollection);
    return collection.lists.ensure(SPConfig.listName, SPConfig.listDescription, 100, false, {
        EnableVersioning: true,
        Hidden: true
    })
    .then(r => {
        // IF SETTINGS LIST EXIST RETURN SETTINGS 
        if (!r.created) {
            return collection.lists
            .getByTitle(SPConfig.listName).items
            .getById(1).select("Settings")
            .usingCaching().get()
            .then(item => {
                config = JSON.parse(item.Settings);
                SETTINGS_LOADED = true;
                return config;
            })
            .catch(console.log);
        } else {
            // CREATE CONFIG LIST - RETURN SETTINGS
            console.log('Creating list');

            // Variable setup 
            var batch = $pnp.sp.createBatch();
            var list = collection.lists.getByTitle(SPConfig.listName);

            // Add settings field 
            var p1 = list.fields.inBatch(batch).addMultilineText(SPConfig.listField, 10, false, false, false, false, undefined)
            .then(() => console.log('created field'))
            .catch(err => {
                console.log(err);
                throw err; // optional: rethrow error to suppress downstream success handling
            });

            // Update View
            var p2 = list.views.getByTitle(SPConfig.listView)
            .fields.inBatch(batch).add(SPConfig.listField)
            .then(() => console.log('update view'))
            .catch(err => {
                console.log(err);
                throw err; // optional: rethrow error to suppress downstream success handling
            });

            // Add default settings
            var p3 = list.items.inBatch(batch).add({
                'Title': SPConfig.listTitle,
                'Settings': JSON.stringify(config)
            })
            .then(() => console.log('created item'))
            .catch(err => {
                console.log(err);
                throw err; // optional: rethrow error to suppress downstream success handling
            });

            // Execute batch
            return Promise.all([p1, p2, p3])
            .then(() => batch.execute())
            .then(() => {
                SETTINGS_LOADED = true;
                return config;
            });
        }
    })
    .catch((err) => {
        console.log(err);
    });
}

其他可行的选择是:

  • 无条件执行var p4 = batch.execute().then(...)return Promise.all([p1, p2, p3, p4])
  • 通过将.then()与{{1}}链接在一起来顺序执行这四个阶段。

你会比我更适合你。