【问题标题】:Promise is not executing correclty?承诺没有正确执行?
【发布时间】:2020-10-09 00:22:18
【问题描述】:

我想在承诺完成后运行_this.install(),它似乎不知道什么是更好的方法?

main.js

async function gitSetUp(path, obj) {
  const _this = obj
  let myFirstPromise = new Promise((resolve, reject) => {
    gitclass.cloneHttps(_this.props.appName)
    setTimeout( function() {
      resolve("Success!")  // Yay! Everything went well!
    }, 250) 
  }) 
  myFirstPromise.then(function(res){
    process.chdir(`${process.cwd()}/${_this.props.appName}`);
    _this.install();
  })

}

GitClass 代码

我最小化代码可能是一些变量没有正确映射只是为了理解promise逻辑

class GitClass {

    cloneHttps(appName) {

        nodegit.Clone.clone(url)
            .then(function(repo) {
                console.log(`Repo ${repo} cloned.`);
                const data = {
                    cloneOptions: cloneOptions,
                    dir: DIR
                }
                return data;
            }).then(function(data) {
                return openRepo(data.dir, data.cloneOptions, appName);
            })
            .catch(function(err) {
                console.log(err);
            });

    }
}

function openRepo(dir, cloneOptions, appName) {

    //getMostRecent master branch commit
    const getMostRecentCommit = function(repository) {
        return repository.getBranchCommit('master').then(function(commit) {
            repoData.commit = commit;
            return repoData;
        });
    };
    // create new branch
    const createBranch = function(data) {
        return data.repo.createBranch(data.branchName, data.commit, 1).then(function(res) {
            data.res = res;
            return data
        });
    }

    // call main node function for promise chaining
    return new Promise(resolve => {
        nodegit.Repository.open(dir)
            .then(getMostRecentCommit)
            .then(createBranch)
            .then(function(res) {
                console.log("Promise Results", res);
                resolve(res);
            })
            .catch(function(err) {
                console.log("ERRR>>>>>>>", err);
            });
    })

}

【问题讨论】:

  • 你有什么错误吗?
  • @losif gitClass.cloneHttps 正在创建新目录,安装作业将在其中完成,安装在导致大量错误的 git 操作之前运行
  • 是什么让你怀疑 git 操作会在 250 毫秒内完成?
  • gitclass.cloneHttps 是否返回一个 Promise?
  • 是的,它正在返回一个承诺

标签: javascript node.js promise


【解决方案1】:

这样的东西有用吗?

async function gitSetUp(path, obj) {
    await gitclass.cloneHttps(obj.props.appName)
    process.chdir(`${process.cwd()}/${obj.props.appName}`);
    obj.install();
}

我不确定你的承诺是否真的需要。 并且const _this = obj 不是必需的:)

如果你想克隆一个 repo,这样的东西应该可以工作

https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

async function gitSetUp(path, obj) {
  return new Promise(resolve => {
   exec(`git clone ${obj.appName}`, null, function() {
    obj.install();
    resolve()
  });
}

【讨论】:

  • 'await' 对这个表达式的类型没有影响。ts(80007)
  • 嗯好吧,应该是同步功能,这里不需要使用promise。
  • 试试child_process.exec的方式?
  • @hussain,您说gitclass.cloneHttps 返回了一个承诺。但是如果你得到这个错误,这意味着它不会返回一个承诺。你能澄清一下吗?
  • @trincot 添加了 Gitclass 代码以理解承诺
【解决方案2】:

如果gitclass.cloneHttps 函数返回一个Promise,那么你不需要myFirstPromise。试试下面的 sn-p。

async function gitSetUp(path, obj) {
  const _this = obj;

  try {
    await gitclass.cloneHttps(_this.props.appName);
  } catch (error) {
    throw error;
  }

  process.chdir(`${process.cwd()}/${_this.props.appName}`);
  _this.install();
}

已编辑: 如果您收到'await' has no effect on the type of this expression.ts(80007),请查看链接'await' has no effect on the type of this expression when using await inside an if block

已编辑: @Hussain cloneHttps 方法没有返回 Promise。

nodegit.Clone.clone(url) 替换为return nodegit.Clone.clone(url)

而且我认为先回调 nodegit.Clone.clone(url) 存在问题(不返回 Promise)

【讨论】:

  • 由于某种原因等待显示错误'await' has no effect on the type of this expression.ts(80007)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2018-10-17
  • 1970-01-01
相关资源
最近更新 更多