GithubHelp home page GithubHelp logo

async operations about sharepointplus HOT 10 CLOSED

aymkdn avatar aymkdn commented on June 12, 2024
async operations

from sharepointplus.

Comments (10)

Aymkdn avatar Aymkdn commented on June 12, 2024

I'm on vacation in another country. I cannot really help for now.

from sharepointplus.

m-o-leary avatar m-o-leary commented on June 12, 2024

I encountered this challenge as well and while I haven't looked at the latest rev of this library, I think my solution may work for you.

var UPDATES = $scope.configurations.map(function(x){
  return new Promise(function(ok, reject) {
    if(updateCheck){
      $SP().list("List Name").update({
        ID:43, Title:"Ok"
      }, {
        success:function(items) {
          ok(items)
        }
      });
    }else{
      $SP().list("List Name").add({
        Title:"Ok"
      }, {
        success:function(items) { 
          ok(items); 
        }
      })
    }
  })
})
Promise.all(UPDATES).then(function(data) {
  console.log('ALL UPDATES OK')
})

Had to use this for a bunch of calls to and API nested within another call - I needed a babel polyfill for Promises as I was using IE11.

I haven't tested this exact code so might not work but the idea is to map and return a bunch of Promises and use Promise .all then to tell when it is all done

from sharepointplus.

Aymkdn avatar Aymkdn commented on June 12, 2024

The last version supports Promise. Also check this link : https://blog.kodono.info/wordpress/2017/04/13/resolve-promise-one-after-another-in-sequence/

from sharepointplus.

micuentadecasa avatar micuentadecasa commented on June 12, 2024

Thanks to all. I'm gonna try your solutions and will tell you..

from sharepointplus.

micuentadecasa avatar micuentadecasa commented on June 12, 2024

@buddythumbs excuse me, if there is an error in the update for example, what happens?
if we add the optional error function what will happen?

from sharepointplus.

m-o-leary avatar m-o-leary commented on June 12, 2024

Hey @micuentadecasa,

Sorry, forgot to put that in there. I would maybe do something like the following:

var UPDATES = $scope.configurations.map(function(x){
  return new Promise(function(ok, reject) {
    if(updateCheck){
      $SP().list("List Name").update({
        ID:43, Title:"Ok"
      }, {
        success:function(items) {
          ok(items)
        }, error:function(items){
          reject(items)
        }
      });
    }else{
      $SP().list("List Name").add({
        Title:"Ok"
      }, {
        success:function(items) { 
          ok(items); 
        }, error:function(items){
          reject(items)
        }
      })
    }
  })
})
Promise.all(UPDATES).then(function(data) {
  console.log('ALL UPDATES OK')
}).catch(function(items){
  console.error(items)
})

(Verify the syntax on the SP Plus docs for adding both success and error)

from sharepointplus.

micuentadecasa avatar micuentadecasa commented on June 12, 2024

Thanks @buddythumbs , your code works like a charm but there was a problem with my first explanation, in fact Promise.all expect all the promises but it doesn't stop execution until all promises are returned that is what I need, I need to wait until all is done or not.

I've seen that for this I will have to work with generators..

from sharepointplus.

micuentadecasa avatar micuentadecasa commented on June 12, 2024

Dear all, I have tried with generators also but no luck (it doesn't stop the execution), I think will be a good idea when @Aymkdn has time to create an article about:

  1. present the idea from @buddythumbs
  2. explain when using sharepointplus functions with an array in $SP().list("List Name").update(array - if it is done in the order of the array of it is done async with any item of the array
  3. propose a solution to do updates or insert in an order (we can have needs of making inserts in an order).
  4. propose a solution to do different operations and not returning until all is done (and stop the execution of the rest of code until we return from this function).

Note:
I know this is out of the library, is more a javascript question but I think it is interesting to people using this library.
In addition know that in ES7 javascript there is an await that can be used for this but it seems it doesn't work in the actual browsers...

from sharepointplus.

Aymkdn avatar Aymkdn commented on June 12, 2024

explain when using sharepointplus functions with an array in $SP().list("List Name").update(array) - if it is done in the order of the array of it is done async with any item of the array

It's done in the order you provide the items into the array.

Example of what you can do:

var add=[], upd=[];
for (var i=0; i<total; i++) {
  if (MyCriteria === true) add.push({MyField:"something"});
  else upd.push({MyField:"something"})
}
// Once you have finished all your checks,
// you can proceed with the add/update operations into the list
// → Example with SharepointPlus v4.0 and Promises
// → in that case the both operations will be done at the same time
// DISCLAIMER: I didn't test it and the Promises stuff in SharepointPlus is new so it might not work as excepted
Promise.all([
  $SP().list("YourList").add(add),
  $SP().list("YourList").update(upd)
]).then(function() {
  // all add and update operations have been completed
})
// or without Promise and if you want to start with ADD and then with UPDATE you can do:
$SP().list("YourList").add(add, {
  after:function() {
    alert("ADD operations completed");
    $SP().list("YourList").update(upd, {
      after:function() { alert("UPDATE operations completed") }
    })
  }
})

Also, if it doesn't answer your question, you need to provide your code and then I should be able to provide a better answer.

from sharepointplus.

micuentadecasa avatar micuentadecasa commented on June 12, 2024

from sharepointplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.