Javascript - 然后函数不执行

时间:2018-01-30 15:17:13

标签: javascript

这是我的问题

const { Client } = require('destiny2');
const client = new Client('apiKey');

window.memberName = function(){

    var pName = document.getElementById('memberName');
    var pId = document.getElementById('memberId');
    var name = document.getElementById('search').value;

    client.getProfile(name, '4')
      .then(data => console.log(data));

    client.getProfile(name, '4').then(
        function(value) { 
            data => pId.appendChild(document.createTextNode("Platform:" + 
              data.profile.platform[1]));
            data => pName.appendChild(document.createTextNode("Name:" + 
             data.profile.displayName));
        }
    );
}

client.getProfile(name, '4').then...此函数中没有任何内容被执行。

2 个答案:

答案 0 :(得分:2)

您的第二次client.getProfile通话错误。

Promise.prototype.then需要onFulfilled功能。

p.then(onFulfilled[, onRejected]);

而不是:

client.getProfile(name, '4').then(
    function(value) { 
        data => pId.appendChild(document.createTextNode("Platform:" + data.profile.platform[1]));
     // ^^^^
     // Remove anonymous function  
        data => pName.appendChild(document.createTextNode("Name:" + data.profile.displayName));
     // ^^^^
     // Remove anonymous function  
    }
);

这样做:

client.getProfile(name, '4').then(data => {
  pId.appendChild(document.createTextNode(`Platform: ${data.profile.platform[1]}`));
  pName.appendChild(document.createTextNode(`Name: ${data.profile.displayName}`));
});

答案 1 :(得分:0)

在lambda中你刚刚定义了2个匿名函数而没有实际执行:

   client.getProfile(name, '4').then(
    function(value) { 
        data => pId.appendChild(document.createTextNode("Platform:" + 
          data.profile.platform[1]));
        data => pName.appendChild(document.createTextNode("Name:" + 
         data.profile.displayName));
    }
   );

用实际通话取代它们