为什么我无法在javascript中更新变量值?

时间:2015-11-15 08:41:19

标签: javascript ajax

好的,我跟着how-to-return-the-response-from-an-asynchronous-call

无论我无法更新变量标签..如何更新标签

function foo() {
    // RETURN the promise
    return fetch("stack-url").then(function(response){
        return response.json(); // process it inside the `then`
    });
}


self.addEventListener('push', function(event){
    var title = 'Featured Message';
    var body = 'StackOverFlow Question Got Featured Post';
    var tag = 'this-is-dummy-tag';

    foo().then(function(response){
      console.log('this is then result ', response);
      var tag = response.question_id;
      return tag;
        // access the value inside the `then`
    })

    event.waitUntil(
      self.registration.showNotification(title, {
        body:body,
        tag:tag,
      })
    );
});

1 个答案:

答案 0 :(得分:0)

尝试包含event.waitUntil.then()个链接,使用第一个tag的返回值设置.then();或者首先设置tag;在,对象

的最后一个值后删除尾随逗号event.waitUntil
    foo().then(function(response){
      console.log('this is then result ', response);
      var tag = response.question_id;
      return tag;
        // access the value inside the `then`
    })
    .then(function(data) {
      event.waitUntil(
        self.registration.showNotification(title, {
          body:body,
          tag:data
        })
      );
    })
    foo().then(function(response){
      console.log('this is then result ', response);
      var tag = response.question_id;

        // access the value inside the `then`
      event.waitUntil(
        self.registration.showNotification(title, {
          body:body,
          tag:tag
        })
      );
    })