in Education by
I am using Angularjs, I have two api in method in factory method. I tried to access that two method one by one. but its not working. We need to first print test1 then print test2. Controller code: app.controlle("Test").function($scope, factoryMethod) { factoryMethod.test1().then(function(data) { console.log(data); console.log("test1"); }).then(factoryMethod.test2().then(function(data) { console.log(data); console.log("test2"); })).catch(function(data) { alert(data); }); } Factory code: app.factory("factoryMethod", function (){ //code for test1 //code for test2 }); Currently the console log print the following order: 1. test2 2. test1 Expecting test1 and than test2 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
it can be done like this. look at snippet var app = angular.module('myapp',[]); app.factory("factoryMethod",['$q',function($q){ var test1 = function(){ var deferred = $q.defer(); deferred.resolve("test1"); return deferred.promise; } var test2 = function(){ var deferred = $q.defer(); deferred.resolve("test2"); return deferred.promise; } return { test1 : test1, test2 : test2 } }]); app.controller("Test",function($scope, factoryMethod){ var promise = factoryMethod.test1(); promise.then(function(data){ console.log(data); factoryMethod.test2().then(function(data){ console.log(data); },function(error){ console.log("error from test 2"); }) },function(error){ console.log("error from test 1") }) });
Run code snippetExpand snippet

Related questions

0 votes
    Warning: Possible Unhandled Promise Rejection (id: 0) TypeError: Object is not a function (evaluating ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I am new to machine learning and I was following this blog on how to write a model with mobilenet ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    anyone know the important questions of c programming tomorrow i have exam frnds.so please help me.promise I will mark brainliest . Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I am trying to create an app using AngularJS that takes a user's location (from their mobile) and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    How can set row number for each table row created by dir-paginate angularJs. I use two way code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am trying to create an app using AngularJS that takes a user's location (from their mobile) and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    What are the differences between AngularJS and Angular?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    AngularJS Vs. Angular 2 Vs. Angular 4: Understanding the Differences...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    What is the difference between AngularJS and Angular?...
asked Feb 25 in Technology by JackTerrance
...