We know that SharePoint JavaScript client object model is asynchronous. However, many a times we want to process things in synchronous way. This can be done by using JavaScript call backs and deferreds/Promises. Given below is an example using call backs:
$(document).ready(function () {
        //don't exectute any jsom until sp.js file has loaded.        
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', prepareTables);
    });

function prepareTables() {
    getItemsWithCaml('External User Account Request',
            function (camlItems) {
                var listItemEnumerator = camlItems.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var listItem = listItemEnumerator.get_current();
                    console.log(listItem.get_item('Title'));
                }
            },
            function (sender, args) {
                console.log('An error occured while retrieving list items:' + args.get_message());
            });
}    

function getItemsWithCaml(listTitle, success, error) {
    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle(listTitle);
    var camlQuery = new SP.CamlQuery();
    var camlItems = list.getItems(camlQuery);
    clientContext.load(camlItems);
    clientContext.executeQueryAsync(
            function () {
                success(camlItems);
            },
            error
        );
};
And here is same example using deferreds/Promises:
$(document).ready(function () {
        //don't exectute any jsom until sp.js file has loaded.        
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', prepareTables);
    });    

function prepareTables() {
    getItemsWithCaml('External User Account Request').then(
            function (camlItems) {
                var listItemEnumerator = camlItems.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var listItem = listItemEnumerator.get_current();
                    console.log(listItem.get_item('Title'));
                }
            },
            function (sender, args) {
                console.log('An error occured while retrieving list items:' + args.get_message());
            }
        );   
}    

function getItemsWithCaml(listTitle) {
    //use of $.Deferred in the executeQueryAsync delegate allows the consumer of this method to write 'syncronous like' code
    var deferred = $.Deferred();
    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle(listTitle);
    var camlQuery = new SP.CamlQuery();        
    var items = list.getItems(camlQuery);
    clientContext.load(items);
    clientContext.executeQueryAsync(
        Function.createDelegate(this,
            function () { deferred.resolve(items); }),
        Function.createDelegate(this,
            function (sender, args) { deferred.reject(sender, args); }));

    return deferred.promise();
};

Scenario: The list contains a single type Lookup field along with several other fields. This lookup field needs to be prepoulated with some value which is passed to the NewForm.aspx page using a query string. Given below is the sample code which reads the query string value of 'AccountReference' and uses this value to select the value of the lookup field named 'External Client Account'. Not this name is the display name of the field which is rendered on the page.
$(document).ready(function () {
    var dropDownValue = getQueryStringParameterByName('AccountReference');    
    if (dropDownValue) {
        $("select[title='External Client Account']").children("option").each(function () {
            if ($(this).text() == dropDownValue) {
                $(this).attr('selected', 'selected');
            }
        });
    }
});

function getQueryStringParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

The Scope of a Web Provisioned event is very important. It determines whether the event is added to SPSite.EventReceivers or SPWeb.EventReceivers collection.

Consider the following code:
using (SPSite site = new SPSite("SiteUrl"))
{
    SPWeb web = site.RootWeb;
    web.EventReceivers.Add(SPEventReceiverType.WebProvisioned, "NY.Publishing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4005dbc2d517460e", "NY.Publishing.EventReceivers.ClientSiteWebEventReceiver.ClientSiteWebEventReceiver");   
    web.Update();
}
As can be seen the Web Provisioned event is added to SPWeb.EventReceivers collection. What this means is that if a site is created under the root site collection, then this Web Provisioned event is called. However, the event will only be called for sub sites directly under root site. For example, suppose we have a site collection named "A". Now we create a sub site named "B" under "A". Then above web provisioned event will be called. Now if we create a sub site under "B" named "C", then the event will not be called. If we have a requirement where we need to call the web provisioned 
event for all sub sites irrespective of the hierarchy, then we need to add the event to SPSite.EventReceivers collection.

I was adding few XsltListViewWebPart web parts programatically on a web part page. But the web parts were not getting added in proper order. Following is the code which adds XsltListViewWebPart to page.
public static void AddXsltListViewWebPartToPage(SPLimitedWebPartManager wpm, string listName, 
    string viewName, string zoneId, int zoneIndex, SPWeb web)
{
    SPList l = web.Lists[listName];
    XsltListViewWebPart xsltWp = new XsltListViewWebPart();
    xsltWp.ListName = l.ID.ToString("B").ToUpper();
    xsltWp.ViewGuid = l.Views[viewName].ID.ToString("B").ToUpper();                
    wpm.AddWebPart(xsltWp, zoneId, zoneIndex);
}
Here is how this code was called to add there web parts on a page in the same zone named "ContentZone":
AddXsltListViewWebPartToPage(webPartManager, "Internal Contacts", "All Contacts", "ContentZone", 0, web);
AddXsltListViewWebPartToPage(webPartManager, "External Contacts", "All Contacts", "ContentZone", 1, web);
AddXsltListViewWebPartToPage(webPartManager, "Other Contacts", "All Contacts", "ContentZone", 2, web);
But "Other Contacts" was getting added before "External Contacts". After some hit and trial I changed the zone index to start from 1 rather than zero and it worked. Here is the updated code:
AddXsltListViewWebPartToPage(webPartManager, "Internal Contacts", "All Contacts", "ContentZone", 1, web);
AddXsltListViewWebPartToPage(webPartManager, "External Contacts", "All Contacts", "ContentZone", 2, web);
AddXsltListViewWebPartToPage(webPartManager, "Other Contacts", "All Contacts", "ContentZone", 3, web);

My SharePoint CodePlex Projects

SharePoint 2010 Autocomplete Lookup Field

Export Version History Of SharePoint 2010 List Items to Microsoft Excel.