开发者问题收集

Javascript 承诺给出 TypeError 函数未定义

2020-04-03
293

以下代码给出了正确的结果。 但是,如果从承诺 sumOfPromises 中删除 then(callback('sum')) 并将其添加到对 sumOfPromises 的调用中(如下所示: sumOfPromises().then(callback('sum')) ),则会出现 TypeError:sumOfPromises(...) 未定义 sumOfPromises 中有一些我无法理解的错误。

<pre>P_1 is : <span id=p_1>p_1</span></pre>
<pre>P_2 is : <span id=p_2>p_2</span></pre>
<pre>Sum is : <span id=sum>sum</span></pre>

<script>
 promise_1().then(callback('p_1'));
 promise_2().then(callback('p_2'));
 sumOfPromises(); // sumOfPromises().then(callback('sum')); gives TypeError: sumOfPromises(...) is undefined (see below)

 function callback(tag) {
     const result = obj => {
	 let e = document.getElementById(tag);
	 e.innerHTML = obj;
     };
     return result
 }

 function sumOfPromises() {
     const promises = [promise_1(), promise_2()];
     
     Promise.all(promises).then(function (data) {
	 return sumOfData(data)
     }).then(callback('sum')); // then moved to top gives TypeError: sumOfPromises(...) is undefined (see upper)
 }
 
 function sumOfData(data) {
     const [p_1, p_2] = data;
     console.log('sumOfData input p_1 '+p_1);
     console.log('sumOfData input p_2 '+p_2);
     
     return new Promise ( (resolve) => {
	 var result = p_1 + p_2;
	 console.log('sumOfData result '+result);
	 resolve(result)
     });
 } 

 function promise_1() {
     return new Promise((resolve) => {
	 const value = Math.random();
	 console.log('promise_1 value '+value);
	 resolve(value);
     })
 }

 function promise_2() {
     return new Promise((resolve, reject) => {
	 const value = - Math.random();
	 console.log('promise_2 value '+value);
	 resolve(value);
     })
 }

</script>
2个回答

您未在 sumOfPromises 中返回任何内容,因此您无法将回调附加到它,因为它 undefined :

 function sumOfPromises() {
     const promises = [promise_1(), promise_2()];

     return Promise.all(promises).then(function (data) { // Add 'return' here
       return sumOfData(data)
     }).then(callback('sum'));
 }
Rashomon
2020-04-03

您需要从 sumOfPromises 返回承诺。由于您的代码中没有 sumOfPromises 中的 return 语句,因此 javaScript 隐式返回 undefined,因此您最终会得到如下调用

undefined.then(callback('sum'));  // which is an error

<pre>P_1 is : <span id=p_1>p_1</span></pre>
<pre>P_2 is : <span id=p_2>p_2</span></pre>
<pre>Sum is : <span id=sum>sum</span></pre>

<script>
 promise_1().then(callback('p_1'));
 promise_2().then(callback('p_2'));
 sumOfPromises().then(callback('sum'));
 
 function callback(tag) {
     const result = obj => {
	 let e = document.getElementById(tag);
	 e.innerHTML = obj;
     };
     return result
 }

 function sumOfPromises() {
     const promises = [promise_1(), promise_2()];
     
     return Promise.all(promises).then(function (data) {
	 return sumOfData(data)
     }) 
 }
 
 function sumOfData(data) {
     const [p_1, p_2] = data;
     console.log('sumOfData input p_1 '+p_1);
     console.log('sumOfData input p_2 '+p_2);
     
     return new Promise ( (resolve) => {
	 var result = p_1 + p_2;
	 console.log('sumOfData result '+result);
	 resolve(result)
     });
 } 

 function promise_1() {
     return new Promise((resolve) => {
	 const value = Math.random();
	 console.log('promise_1 value '+value);
	 resolve(value);
     })
 }

 function promise_2() {
     return new Promise((resolve, reject) => {
	 const value = - Math.random();
	 console.log('promise_2 value '+value);
	 resolve(value);
     })
 }

</script>
Code Maniac
2020-04-03