Return a Promise that promises to throw error

给你一囗甜甜゛ 提交于 2019-12-02 09:45:52

You could inject $q service and return $q.reject(reason), e.g:

return $q.reject('NO_INDEX');

From $q documentation:

Creates a promise that is resolved as rejected with the specified reason.

You just need to return a rejected promise with $q.reject(err):

return {

   search: function(indexName, searchTerms){
       var index = indices[indexName];

       if(index){
          return index.search(searchTerms); //returns a promise
       }
       else{
          // return rejected promise if no match
          return $q.reject(new Error("no matching index"));
       }
   }

Documentation here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!