import load from './load';

export default function update(req) {
  return new Promise((resolve, reject) => {
    // write to database
    setTimeout(() => {
      if (Math.random() < 0.2) {
        reject('Oh no! Widget save fails 20% of the time. Try again.');
      } else {
        const widgets = load(req);
        const widget = req.body;
        if (widget.color === 'Green') {
          reject({
            color: 'We do not accept green widgets' // example server-side validation error
          });
        }
        if (widget.id) {
          widgets[widget.id - 1] = widget;  // id is 1-based. please don't code like this in production! :-)
        }
        resolve(widget);
      }
    }, 1500); // simulate async db write
  });
}

@@ -271,16 +271,51 @@
  else %7B%0A
+        load(req).then(data =%3E %7B%0A  
         
@@ -330,25 +330,20 @@
 dgets = 
-load(req)
+data
 ;%0A      
@@ -336,32 +336,34 @@
 = data;%0A        
+  
 const widget = r
@@ -371,16 +371,18 @@
 q.body;%0A
+  
         
@@ -419,24 +419,26 @@
 %7B%0A          
+  
 reject(%7B%0A   
@@ -434,16 +434,18 @@
 eject(%7B%0A
+  
         
@@ -538,16 +538,18 @@
         
+  
 %7D);%0A    
@@ -544,34 +544,38 @@
     %7D);%0A        
-%7D%0A
+  %7D%0A  
         if (widg
@@ -593,16 +593,18 @@
         
+ 
+ 
 widgets%5B
@@ -694,16 +694,61 @@
 on! :-)%0A
+            req.session.widgets = widgets;%0A  
         
@@ -757,16 +757,18 @@
         
+  
 resolve(
@@ -776,16 +776,28 @@
 idget);%0A
+        %7D);%0A
       %7D%0A

Fix update widgets api to actually save to session