You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含if判断的Promise链式then块未执行,求技术解答

Why Your Promise Chain's then() Blocks Aren't Executing

Hey there! Let's walk through exactly what's going wrong with your code and how to fix it—you're hitting two key Promise pitfalls here.

1. Your Inner Promise Never Gets Settled (The Main Culprit)

Looking at your nested new Promise call:

new Promise(function (resolve) {
  if (i == 0) {
    return new pTree().renamePage('Home', name);
  } else {
    return edit.addPage(name, '3D')
  }
})

The problem here is that you're returning another Promise inside the executor function, but never calling resolve() or reject() to signal the inner Promise's completion.

Promise executors require you to explicitly call resolve (for success) or reject (for failure) to move the Promise out of the pending state. Your inner Promise is stuck forever in pending, so none of the subsequent .then() blocks attached to it will ever run.

Fix for This Issue

You have two clean options here:

  • Option 1: Remove the unnecessary nested Promise (best practice, since renamePage and addPage already return Promises):
    Instead of wrapping them in another new Promise, just use those existing Promises directly in your chain.
  • Option 2: Resolve the inner Promise with the result of the nested call:
    If you need to keep the inner Promise (for some custom logic), call resolve() with the returned Promise, which will chain their states.

2. Loop Variable Scoping (var vs let)

Another hidden issue: you're using var i in your for loop. Since var has function-level scope (not block-level), by the time your Promise executors run asynchronously, the loop will have already finished, and i will equal inputConfig.length for every Promise. That means your if (i == 0) check will almost never behave as expected.

Fix this by replacing var i with let i—let creates a new binding for each loop iteration, so each Promise will capture the correct value of i.

Fixed Code Example

Here's the revised code addressing both issues:

var uploadResourceToView = function (edit) { 
  var promises = []; 
  // Use let instead of var for block-scoped i
  for (let i = 0; i < inputConfig.length; i++) { 
    const name = 'hey';
    // Start with the appropriate page promise directly, no extra wrapper
    const pagePromise = i == 0 
      ? new pTree().renamePage('Home', name) 
      : edit.addPage(name, '3D');
    
    // Build the chain and push to promises array
    promises.push(
      pagePromise
        .then(function () { 
          console.log('Now I run!'); 
          return edit.addObject(object); 
        }) 
        .then(function () { 
          return edit.addResource('test resource', true); 
        })
    );
  } 
  return Promise.all(promises); 
}

Key Changes Explained:

  • Replaced var i with let i to fix loop scoping.
  • Removed the redundant inner new Promise wrapper—we start directly with the Promise returned by renamePage or addPage.
  • The chain now properly flows: each .then() runs only after the previous Promise resolves, since we're chaining directly off the settled page operation Promise.

Bonus: Using Arrow Functions for Cleaner Code

If you prefer, you can make the code more concise with arrow functions:

const uploadResourceToView = (edit) => { 
  const promises = inputConfig.map((_, i) => { 
    const name = 'hey';
    const pagePromise = i === 0 
      ? new pTree().renamePage('Home', name) 
      : edit.addPage(name, '3D');
    
    return pagePromise
      .then(() => { 
        console.log('Now I run!'); 
        return edit.addObject(object); 
      }) 
      .then(() => edit.addResource('test resource', true));
  }); 
  return Promise.all(promises); 
}

内容的提问来源于stack exchange,提问作者fullstack_realm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:06:45