在JavaScript中,Promise的then方法是异步执行的,它接受一个回调函数作为参数。如果需要在then方法内调用其他函数,可以将该函数作为回调函数传递给then方法。
以下是一个示例代码:
function myFunction() {
return new Promise(function(resolve, reject) {
// 异步操作
setTimeout(function() {
resolve("成功");
}, 1000);
});
}
function anotherFunction() {
console.log("另一个函数被调用了");
}
myFunction().then(function(result) {
console.log(result);
anotherFunction(); // 在then方法内调用另一个函数
});
在上面的代码中,myFunction函数返回一个Promise对象,通过setTimeout模拟异步操作。在then方法的回调函数中,可以调用anotherFunction函数。
需要注意的是,由于then方法是异步执行的,因此调用anotherFunction函数的时间会在Promise的异步操作完成后。