.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - AngularJs - defer resolve object - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJs - defer resolve object - Stack Overflow

programmeradmin1浏览0评论

i've a function that return a boolean according to http.get call.

function checkValue (value) {
    var defer = $q.defer();
    var request = $http({
          method: "get",
          url: "someurl"
    });

      request.success(function (data) {
        defer.resolve(data.response);

    });
    return defer.promise;
}

The problem is that return value is an object like this:

d {$$state: Object} $$state: Object status: 1 value: true __proto__: Object __proto__: d

How could i resolve it?

i've a function that return a boolean according to http.get call.

function checkValue (value) {
    var defer = $q.defer();
    var request = $http({
          method: "get",
          url: "someurl"
    });

      request.success(function (data) {
        defer.resolve(data.response);

    });
    return defer.promise;
}

The problem is that return value is an object like this:

d {$$state: Object} $$state: Object status: 1 value: true __proto__: Object __proto__: d

How could i resolve it?

Share Improve this question asked Feb 9, 2016 at 17:48 Ethan HuntEthan Hunt 1834 silver badges17 bronze badges 1
  • Did you try using the then callback? checkValue(...).then(function(value) { ... } – paul trone Commented Feb 9, 2016 at 17:51
Add a ment  | 

3 Answers 3

Reset to default 4

Yes, your function is returning a Promise object. The $$state property belongs to the promise object and is used by Angular internally (as suggested by $$ prefix) and therefore not to be used by developers using Angular. More interesting is that promise object has a "then" method which can be used to attach handlers that are called when the promise gets resolved. Promise gets resolved when the defer object related to the promise is resolved.

So, you could use your code as

checkValue.then(function (data) {
    // This is called when the XHR response es in and
    // defer.resolve() is called in the checkValue function.
});

But, there is a better approach to this. Using promise chaining.

Which essentially removes the need to create a new defer object.

function checkValue (value) {
    return $http({
          method: "get",
          url: "someurl"
    }).then(function (data) {
        return data.response;
    });
}

Explaining everything about how Promises work is may be too much for this post. There are already awesome works done on that.

But, basically, $http already returns a promise. So you can just use this promise instead of creating a new one. The end usage of the function remains exactly the same as the snippet above.

BTW, .success() handlers are deprecated. So better start using .then() on your apps already.

Since you are using Promise, you need to use .then callback in order to get response or error:

function checkValue (value) {
    var defer = $q.defer();
    var request = $http({
          method: "get",
          url: "someurl"
    });

    request.success(function (data) {
      defer.resolve(data.response);    
    });
    return defer.promise;
}    

var promise = checkValue('Hello World');
promise.then(function(response) {
  //success
  console.log(response);
}, function(reason) {
  //failed
  console.log(reason);
});

then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.

But the optimized approach would be using $http's inbuilt promise instead of creating a new one.

var request = $http({
  method: 'GET',
  url: '/someUrl'
});

request.then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

You should try this,

function checkValue (value) {
return $q(resolve, reject) {
$http({
      method: "get",
      url: "someurl"
}).success(function (data) {
    resolve(data.response);
}).then (function (error) {
    reject(error);
});
}
}

This will resolve your data

发布评论

评论列表(0)

  1. 暂无评论